I would like understand the difference between then callback and success callback when called over http get. When i use then callback it returns the data but with success callback it doesnt. Below is the code
我想了解当通过http get调用时回调和成功回调之间的区别。当我使用然后回调它返回数据,但成功回调它不。以下是代码
Then callback
$http.get(url).
then(function(response) {
response.data.data;});
Success callback
$http.get(url).
success(function(response) {
response.data;});
3 个解决方案
#1
6
Your issue seem to be around this:
您的问题似乎与此有关:
$http.get('/someUrl'). success(function(data, status, headers, config) {
$ http.get( '/ someUrl')。成功(函数(数据,状态,标题,配置){
it's a different return from then
,
这是一个不同的回报,
then
method to register callbacks, and these callbacks will receive a single argument – an object representing the response然后注册回调的方法,这些回调将接收一个参数 - 一个表示响应的对象
In other words, you should be doing this:
换句话说,你应该这样做:
$http.get(...).success(function(data){ console.log(data) })
$http.get(...).then(function(response){ console.log(response.data) })
And of course the chaining differences, but doesn't seem related to your issue:
当然还有链接差异,但似乎与您的问题无关:
then()
If you chain then()
, the callbacks will run sequentially after each one is done, because it returns a new promise object on each chain
如果你链接then(),回调将在每一个完成之后顺序运行,因为它在每个链上返回一个新的promise对象
success()
(deprecated* along with error()
)
If you chain success()
calls, the callbacks will be ran in parallel, because it returns the original promise object
如果链接success()调用,则回调将并行运行,因为它返回原始的promise对象
*success
and error
are deprecated, see Deprecation Notice section in $http docs
*不推荐使用成功和错误,请参阅$ http docs中的弃用通知部分
#2
0
you can use any of .then or .success and the call back code depends on what method you are using.
您可以使用.then或.success中的任何一个,回调代码取决于您使用的方法。
.then() will have two arguments first is the success handler and second is the error handler.success handler inside then() can be written in some other way nothing but .success.the main diffrence between successhandler inside then() and .success function is .success will have 4 arguments(data,status,headers,config) where as success handler in then() will have only one argument which has(data,status,headers,config) embeded in that one argument nothinf but you can access it like(response.data,response.status etc...)
.then()将有两个参数,第一个是成功处理程序,第二个是错误handler.success处理程序里面then()可以用其他方式编写,但是.success.the success()和.success中的successhandler之间的主要差异函数是.success将有4个参数(数据,状态,标题,配置),其中then()中的成功处理程序将只有一个参数,其中包含(data,status,headers,config)一个参数nothinf但你可以访问它(response.data,response.status等...)
#3
0
Important recommendation from angularjs.org:
angularjs.org的重要建议:
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions
is set to false then these methods will throw $http/legacy error.
$ http遗留承诺方法成功和错误已被弃用。请改用标准方法。如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误。
#1
6
Your issue seem to be around this:
您的问题似乎与此有关:
$http.get('/someUrl'). success(function(data, status, headers, config) {
$ http.get( '/ someUrl')。成功(函数(数据,状态,标题,配置){
it's a different return from then
,
这是一个不同的回报,
then
method to register callbacks, and these callbacks will receive a single argument – an object representing the response然后注册回调的方法,这些回调将接收一个参数 - 一个表示响应的对象
In other words, you should be doing this:
换句话说,你应该这样做:
$http.get(...).success(function(data){ console.log(data) })
$http.get(...).then(function(response){ console.log(response.data) })
And of course the chaining differences, but doesn't seem related to your issue:
当然还有链接差异,但似乎与您的问题无关:
then()
If you chain then()
, the callbacks will run sequentially after each one is done, because it returns a new promise object on each chain
如果你链接then(),回调将在每一个完成之后顺序运行,因为它在每个链上返回一个新的promise对象
success()
(deprecated* along with error()
)
If you chain success()
calls, the callbacks will be ran in parallel, because it returns the original promise object
如果链接success()调用,则回调将并行运行,因为它返回原始的promise对象
*success
and error
are deprecated, see Deprecation Notice section in $http docs
*不推荐使用成功和错误,请参阅$ http docs中的弃用通知部分
#2
0
you can use any of .then or .success and the call back code depends on what method you are using.
您可以使用.then或.success中的任何一个,回调代码取决于您使用的方法。
.then() will have two arguments first is the success handler and second is the error handler.success handler inside then() can be written in some other way nothing but .success.the main diffrence between successhandler inside then() and .success function is .success will have 4 arguments(data,status,headers,config) where as success handler in then() will have only one argument which has(data,status,headers,config) embeded in that one argument nothinf but you can access it like(response.data,response.status etc...)
.then()将有两个参数,第一个是成功处理程序,第二个是错误handler.success处理程序里面then()可以用其他方式编写,但是.success.the success()和.success中的successhandler之间的主要差异函数是.success将有4个参数(数据,状态,标题,配置),其中then()中的成功处理程序将只有一个参数,其中包含(data,status,headers,config)一个参数nothinf但你可以访问它(response.data,response.status等...)
#3
0
Important recommendation from angularjs.org:
angularjs.org的重要建议:
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions
is set to false then these methods will throw $http/legacy error.
$ http遗留承诺方法成功和错误已被弃用。请改用标准方法。如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误。