The .success
syntax was correct up to Angular v1.4.3.
For versions up to Angular v.1.6, you have to use then
method. The then()
method takes two arguments: a success
and an error
callback which will be called with a response object.
Using the then()
method, attach a callback
function to the returned promise
.
Something like this:
app.controller('MainCtrl', function ($scope, $http){ $http({ method: 'GET', url: 'api/url-api' }).then(function (response){ },function (error){ }); }
See reference here.
Shortcut
methods are also available.
$http.get('api/url-api').then(successCallback, errorCallback); function successCallback(response){ //success code } function errorCallback(error){ //error code }
The data you get from the response is expected to be in JSON
format. JSON is a great way of transporting data, and it is easy to use within AngularJS
The major difference between the 2 is that .then()
call returns a promise
(resolved with a value returned from a callback
) while .success()
is more traditional way of registering callbacks
and doesn’t return a promise
.