$ http.get (...). Success is not a function

I have this code:

app.controller('MainCtrl', function ($scope, $http){ $http.get('api/url-api') .success(function (data, status, headers, config){ } } 

It works fine in my local environment, but it will return this error on the server:

TypeError: $ http.get (...). Success is not a function

Any ideas? Thanks

+52
json javascript angularjs promise ajax
Dec 15 '16 at 16:51
source share
3 answers

The .success syntax was correct before Angular v1.4.3.

For versions prior to Angular v.1.6, you must use the then method. The then() method takes two arguments: a success and an error callback that will be called with the response object.

Using the then() method, attach the callback function to the returned promise .

Something like that:

 app.controller('MainCtrl', function ($scope, $http){ $http({ method: 'GET', url: 'api/url-api' }).then(function (success){ },function (error){ }); } 

See link here.

Shortcut also available.

 $http.get('api/url-api').then(successCallback, errorCallback); function successCallback(response){ //success code } function errorCallback(error){ //error code } 

It is expected that the data received from the response will be in JSON . JSON is a great way to transfer data and is easy to use in AngularJS

The main difference between 2 is that the .then() call returns a promise (resolved with the value returned from the callback ), and .success() is a more traditional way of registering callbacks and does not return a promise .

+110
Dec 15 '16 at 16:56
source

This may be redundant, but the highest-voted answer says .then(function (success) , and it didn’t work for me, as in Angular version 1.5.8 . Instead, use response , then inside my response.data block you get 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.'); }); 
+1
May 04 '17 at 9:22 pm
source

If you are trying to use AngularJs 1.6.6 from 10/21/2017, the following option works as .success and has been exhausted. The .then () method takes two arguments: the response and the error callback that will be called with the 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 the login page.

0
Oct 21 '17 at 15:02
source



All Articles