i have this code:
app.controller('MainCtrl', function ($scope, $http){
$http.get('api/url-api')
.success(function (data, status, headers, config){
}
}
In my local enviroment, works ok, but in a server, return this error:
TypeError: $http.get(...).success is not a function
Any ideas? Thanks
This might be redundant but the above most voted answer says
.then(function (success)
and that didn't work for me as of Angular version1.5.8
. Instead useresponse
then inside the blockresponse.data
got me my json data I was looking for.If you are trying to use AngularJs 1.6.6 as of 21/10/2017 the following parameter works as .success and has been depleted. The .then() method takes two arguments: a response and an error callback which will be called with a response object.
The above snipit works for a login page.
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. Thethen()
method takes two arguments: asuccess
and anerror
callback which will be called with a response object.Using the
then()
method, attach acallback
function to the returnedpromise
.Something like this:
See reference here.
Shortcut
methods are also available.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 AngularJSThe major difference between the 2 is that
.then()
call returns apromise
(resolved with a value returned from acallback
) while.success()
is more traditional way of registeringcallbacks
and doesn't return apromise
.