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
Undefined function: .succes in $http.post (angular.js)
提问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
。
$http
is promised based, so you can also use:.then
for success,.catch
for fail and.finally
in both cases.
$http
是基于承诺的,所以你也可以使用:.then
成功,.catch
失败,.finally
在这两种情况下。
See:
看:
回答by mcompeau
There is a typo in the succes
function name. Despite that, the success
and error
functions are now removed as of Angular 1.6 and should be replaced with the standard promise functions .then()
/ .catch()
.
succes
函数名有错别字。尽管如此,success
和error
函数现在从 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 then
for $http promises.
它then
用于 $http 承诺。
At second, you have a typo, it should be success
其次,你有一个错字,应该是 success