ajax success then,ajax - Angular JS success works but not then, why? - Stack Overflow

What is the difference between success and then?

then resolves an object that represents your http response. That is to say, the callback of the then method take only one argument that has the following structure:

data – {string|Object} – The response body transformed with the transform functions.

status – {number} – HTTP status code of the response.

headers – {function([headerName])} – Header getter function.

config – {Object} – The configuration object that was used to generate the request.

statusText – {string} – HTTP status text of the response.

On the other hand, success is a shorthand function that will spread the different properties of the http response (except the statusText that is not very useful) into different arguments. Therefore the first argument of the success callback will contain only the response body (the property response.data) of the http response.

The following piece of code is taken from angular 1.4.5, and I was able to find it since angular 1.0.0:

promise.success = function(fn) {

promise.then(function(response) {

fn(response.data, response.status, response.headers, config);

});

return promise;

};

When to use "then", and when to "success"?

For consistency with other promise libraries, I'll advise you to always use the standard then method. If you replace $http.success with $http.then be careful that what you got in the response object in the former, is equivalent to response.data in the latter:

$http.get(...).success(function (response) {

return response;

}

is equivalent to:

$http.get(...).then(function (response) {

return response.data;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值