Sending message request and getting error & ldquo; undefined is not a & rdquo;

advertisements

I created an AngularJs app.

I have a controller and when i click on a specific button i need to send information to server. The controller has a function called "button_clicked"

$scope.button_clicked = function () {

    1     var currentObject = $scope.getCurrentObject;

    2    $http.post(ENV.server_prefix + 'object/addObject/', currentObject)
    3        .then(function (data, status) {
    4          //Doing something!!
    5       }).error(function (data, status, params) {
    6           alert(data);
    7       });
    };

When i am running the code, the callback is executed. However in console in get an error: "undefined is not a function" and it points to line 5

Why this error and how to handle it?


Try using success:

$scope.button_clicked = function () {

     var currentObject = $scope.getCurrentObject;

    $http.post(ENV.server_prefix + 'object/addObject/', currentObject)
        .success(function (data, status) {
          //Doing something!!
       }).error(function (data, status, params) {
           alert(data);
       });
};

When you use then, the error handler should be passed as a second argument:

$http.post(ENV.server_prefix + 'object/addObject/', currentObject)
    .then(function (data, status) {
      //Doing something!!
   }, function (data, status, params) {
       alert(data);
});