Reputation: 455
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
Then callback
$http.get(url).
then(function(response) {
response.data.data;});
Success callback
$http.get(url).
success(function(response) {
response.data;});
Upvotes: 6
Views: 5586
Reputation: 55448
Your issue seem to be around this:
$http.get('/someUrl'). success(function(data, status, headers, config) {
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
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
and error
are deprecated, see Deprecation Notice section in $http docs
Upvotes: 6
Reputation: 644
Important recommendation from 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.
Upvotes: 0
Reputation: 11
you can use any of .then or .success and the call back code depends on what method you are using.
.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...)
Upvotes: 0