【问题标题】:Undefined function: .succes in $http.post (angular.js) [closed]未定义的函数:$http.post(angularjs)中的.success [关闭]
【发布时间】:2014-10-10 05:12:26
【问题描述】:

我一直在尝试使用 Angular JS 进行搜索 但我收到了这个奇怪的错误:TypeError: undefined is not a function 它是.success

这是我的代码:

<html ng-app="app">
<head>
<meta charset="utf-8">
<script src="angular.js"></script>
<script>
  var app = angular.module('app', []);

  app.controller('ctrl', function ($scope, $http){
      $scope.url = 'fetch.php';
      $scope.search = '';

      $scope.postLink = function(){

        $http.post($scope.url, { "data" : $scope.search}).succes(function(data, status){
          console.log(data);
        }).error(function(data, status) {
          $scope.data = data || "Request failed";
          $scope.status = status;   
        });
      }
  });

</script>
</head>
<body ng-controller="ctrl">
    <input type="text" ng-model="search">
    <input type="submit" ng-click="postLink()">
</body>
</html>

谁能告诉我为什么它在 .succes 上给我一个错误?我不能在那里打电话吗?

提前致谢!

【问题讨论】:

  • 拼写为success ...
  • 哇...真的...羞愧羞愧我...谢谢!
  • 自 Angular 1.6 起,successerror 方法已从 Angular 中删除。请参阅下面我的答案中的详细信息。

标签: javascript php json angularjs post


【解决方案1】:

拼写为success

$http 是基于承诺的,因此您也可以使用:
.then 表示成功,
.catch 表示失败,
.finally 在这两种情况下。

见:

【讨论】:

  • 谢谢先生!会调查的。错字已经解决了我的问题。不敢相信我没有注意到这一点。
【解决方案2】:

succes 函数名中有错字。尽管如此,successerror 函数现在已从 Angular 1.6 中删除,应替换为标准承诺函数 .then() / .catch()

此答案中的更多详细信息:Why are angular $http success/error methods deprecated? Removed from v1.6?

【讨论】:

    【解决方案3】:

    $http 承诺是 then

    第二个,你打错了,应该是success

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多