简体   繁体   English

发送帖子请求并出现错误“未定义不是函数”

[英]Sending post request and getting error “undefined is not a function”

I created an AngularJs app. 我创建了一个AngularJs应用。

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" 控制器具有称为“ 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 但是在控制台中出现错误:“未定义不是函数”,它指向第5行

Why this error and how to handle it? 为什么会出现此错误以及如何处理?

Try using success : 尝试使用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: 当您使用then ,错误处理程序应作为第二个参数传递:

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

You're mixing up the syntax for then here. 你混淆的语法, then在这里。 If you use then it expects two arguments, the first being a success-handler and the second being an error-handler. 如果使用, then需要两个参数,第一个是成功处理程序,第二个是错误处理程序。

$scope.button_clicked = function () {

    var currentObject = $scope.getCurrentObject;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM