Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http.get difference between then and success callback

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;});
like image 355
Vaishu13 Avatar asked Jan 17 '15 13:01

Vaishu13


1 Answers

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

like image 51
bakkal Avatar answered Oct 09 '22 22:10

bakkal