Angularjs what is the differences of handling ajax from success/error and promise? -
i'm not sure should handle response of ajax request.
there 2 flavours.
- handle via success/error callback $http.
- handle via then() method promise result.
both responds expected. but, suppose there 'catch' use each one. google didn't show me way. , angular.js source code kinda... cryptic me.
notice: example (1) responds first , (2) responds next. think because localhost latency null , both async method.
$http({ method: 'post', url: 'ping.php', headers: {'content-type' : 'application/json'} }). success(function(data, status, headers, config) { console.log("flavour 1 success"); }). error(function(data, status, headers, config) { console.log("flavour 1 error"); }).then(function() { console.log("flavour 2 success"); }, function() { console.log("flavour 2 error");
});
according angular documentation,
{httppromise}
– returns promise object standardthen
method , 2 http specific methods:success
,error
.then
method takes 2 arguments:success
,error
callback called response object.success
,error
methods take single argument - function called when request succeeds or fails respectively.
to answer question, difference success
, error
take 1 argument, whereas then
takes 2 arguments.
the first invoked before second, because you've listed in order, , that's how promises work. if had listed then
, invoked third.
Comments
Post a Comment