$http.get(...).success is not a function
Asked Answered
S

3

127

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

Semiprofessional answered 15/12, 2016 at 16:51 Comment(9)
what version on local envm and on server? btw, $http.get return HttpPromise, so you need use then insteadKlotz
have you checked that all your javascripts loads in the server environment?Banian
its then() not success()Foltz
Grundy, Version is v1.5.8, bansi Yes, all javascript are loaded. Thanks!Semiprofessional
@AlejoRibes, 1.5.8 on both environment? are you sure, that on local and server same code?Klotz
Grundy, yes, is the same codeSemiprofessional
Look at the documentation: docs.angularjs.org/api/ng/service/$httpIngratiating
The .success syntax was correct up to Angular v1.4.3. See the old docs here: code.angularjs.org/1.4.3/docs/api/ng/service/$httpCrossness
and officially removed in v.1.6Flats
D
244

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.

Dixil answered 15/12, 2016 at 16:56 Comment(4)
I tryed with .then and works ok, thanks Alexandru-Ionut MihaiSemiprofessional
.success and .then take different param, account for thatWorkwoman
If re-writing the existing code, it may be easy to present the two arguments-functions (success, error) mentioned above in-line, and not separately like in the example.Shelve
"$resource(...).get(...).then is not a function" ... Why is angularJS so crappy when it comes to consistency?Resplendent
S
9

This might be redundant but the above most voted answer says .then(function (success) and that didn't work for me as of Angular version 1.5.8. Instead use response then inside the block response.data got me my json data I was looking for.

$http({
    method: 'get', 
    url: 'data/data.json'
}).then(function (response) {
    console.log(response, 'res');
    data = response.data;
},function (error){
    console.log(error, 'can not get data.');
});
Searcy answered 4/5, 2017 at 21:22 Comment(5)
i mean... did you try success.data? the parameter name isn't that important in this case.Marika
My code works. When I followed the above answer I got stuck. It is also an answer with a way to actually get the data and log it to your console. This can show developers how to test their data in their browser. I was led here by the same exact error in the question headline.Searcy
old code $http.get('data/data.json').success(function(data) { data = data;} with my answer a developer now knows its data.data can't just get data by itself. hence my answer is important to this error message.Searcy
The variable name will not make any difference, it could be success.data or response.data or anything else. You could even use donaldTrump.data that will work too. Although you should use sensible variable names, not sure this one will make much sense.Elnora
This is because the success object has an array named data that holds the data coming as response from your server. you need to access that data array, using <yourSuccessObjectName>.dataElnora
B
4

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.

 $scope.login = function () {
        $scope.btntext = "Please wait...!";
        $http({
            method: "POST",
            url: '/Home/userlogin', // link UserLogin with HomeController 
            data: $scope.user
         }).then(function (response) {
            console.log("Result value is : " + parseInt(response));
            data = response.data;
            $scope.btntext = 'Login';
            if (data == 1) {
                window.location.href = '/Home/dashboard';
             }
            else {
            alert(data);
        }
        }, function (error) {

        alert("Failed Login");
        });

The above snipit works for a login page.

Billat answered 21/10, 2017 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.