javascript - Why can't I return $.ajax result but can return $http.post result? -
i have 2 return statements:
return $http.post({ url: cheapwatcher.config.domain + 'api/authenticate', contenttype: 'application/x-www-form-urlencoded; charset=utf-8', data: data }); return $.ajax({ type: 'post', url: cheapwatcher.config.domain + 'api/authenticate', data: data }).done(function (result) { console.log('logged successfuly'); }).fail(function (result) { console.log('loging failed'); });
and background.js function uses api methods:
// register api command listener chrome.runtime.onconnect.addlistener(function (port) { port.onmessage.addlistener(function (request, portinfo) { // globally accessible function execure api calls cheapwatcher.executeapicall = function (request, senderid) { var originalrequestmethod = request.method; //dinamically call api method cheapwatcher.api[request.method](request, senderid).then(function (response) { port.postmessage(response); }, function (error) { port.postmessage(error); }); }; cheapwatcher.executeapicall(request, request.sender); }); });
so ofcourse when i'm running chrome extension comment 1 of them, point both of them return same object type jqxhr , if i'm using $http.post gives me these errors:
error in event handler (unknown): typeerror: cannot read property 'error' of null
and
post chrome-extension://cmakfpdagfeiefcmmpmhjrtyhonmgnbi/background/[object%20object] net::err_file_not_found
and when i'm using $.ajax works, token needed gives me error:
error in event handler (unknown): typeerror: cannot read property 'error' of null
and after extension can't continue working. yesterday asked why $.ajax gives me error , got answer because can't return jqxhr type, if i'm using
return $http.post(cheapwatcher.config.domain + 'api/authenticate', data);
everything fine post method wants give him grant_type=password , content-type application/x-www-form-urlencoded that's why used $http.post([settings]) structure.
can please explain me how both work , why i'm getting these errors? maybe i'm using post method incorrectly or need declare more don't know?
you're not using $http.post correctly. method take 3 parameters not 1 object :
return $http.post( cheapwatcher.config.domain + 'api/authenticate', transformdatatourlencoded(data), {contenttype: 'application/x-www-form-urlencoded; charset=utf-8'} });
as can see put transformdatatourlencoded. because angular serialize in json doesn't antively transform javascript object urlencoded.
from old answer out of subject
$http.post return promise on can use .then. angular promise chanable, meaning can .then().then().then() again , again have multiple layer of processing result. of course don't need often. never 2 level :
- processing result server , format data in service layer
- using result in controller layer
i don't know jquery guess that's it's not chainable angular.
you can
.done().fail()
because .done return on object on can perform .fail() not
.done().done().
because object returned .done doesn't have .done() method.
finally .fail() terminate point : return nothing return null value.
Comments
Post a Comment