Search code examples
angularjsangularjs-http

AngularJS $http success function not working


I am very new on AngularJS and I am following a tutorial to send REST request to a server. This is my code

angular.module('myApp')
  .controller('MainCtrl', ['$scope','$window','$http', function($scope,$window,$http) 
  {
$scope.submitForm = function() {
  var url = 'http://localhost:8080/Server/config/start';
  var request = $http({
    method: 'POST',
    url: url,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest: function(obj) {
      var str = [];
      for(var p in obj)
        str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
      return str.join('&');
    },
    data: {gsvalue: $scope.name}}).success(function () {});
} 

}]);

I receive properly the request on the server side but I am not able to get the response, the success function don't get called. I also get two warnings on the console

Error: $http(...).success is not a function

and

Error: JSON.parse: unexpected character at line 1 column 1 of the JSON data
.......
Possibly unhandled rejection: {}

I found some similar subjects but I was not able to solve my problem with them.


Solution

  • success and error were removed in 1.6. There are some reasons for that: Why are angular $http success/error methods deprecated? Removed from v1.6?.

    Use then, catch and finally, which are standard for Promises.