javascript 未定义的函数:.succes in $http.post (angular.js)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25343831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:24:14  来源:igfitidea点击:

Undefined function: .succes in $http.post (angular.js)

javascriptphpjsonangularjspost

提问by Consolee Logg

I've been trying to make a search with Angular JS but I get this weird error: TypeError: undefined is not a function and it is on the .success

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

Here is my code:

这是我的代码:

<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>

Can anyone tell my why it gives me an error on the .succes? Am I not allowed to call it there?

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

Thanks in advance!

提前致谢!

回答by jantimon

It is spelled success.

它是拼写的success

$httpis promised based, so you can also use:
.thenfor success,
.catchfor fail and
.finallyin both cases.

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

See:

看:

回答by mcompeau

There is a typo in the succesfunction name. Despite that, the successand errorfunctions are now removed as of Angular 1.6 and should be replaced with the standard promise functions .then()/ .catch().

succes函数名有错别字。尽管如此,successerror函数现在从 Angular 1.6 开始被移除,应该替换为标准的 promise 函数.then()/ .catch()

More details in this answer: Why are angular $http success/error methods deprecated? Removed from v1.6?

此答案中的更多详细信息:为什么不推荐使用 angular $http 成功/错误方法?从 v1.6 中删除?

回答by Guard

It's thenfor $http promises.

then用于 $http 承诺。

At second, you have a typo, it should be success

其次,你有一个错字,应该是 success