[英]$http.post is not a function
I am trying to submit a form data to an API endpoint which I created. 我正在尝试将表单数据提交给我创建的API端点。 I have tested it in PostMan and the API functions well and I can get the data in successfully. 我已经在PostMan中测试了它,并且API功能很好,我可以成功获取数据。 But while connecting that API endpoint to a function in angular js I get the following error. 但是当将该API端点连接到角度js中的函数时,我收到以下错误。
Heres my code: 继承我的代码:
$scope.saveSession = function() {
$http.post("/session/survey", $scope.session).success(function(data, status) {
$window.location.href = '/';
console.log("Sucessfully getting data" + JSON.stringify(data));
})
}
Note: 注意:
$scope.session
is an object that being populated by using the ng-model
tag. $scope.session
是使用ng-model
标记填充的对象。 For example: 例如:
<input type="text" ng-model="session.title">
Edit (Controller Code): 编辑(控制器代码):
// This is our controller for the bio page
var session = angular.module('session', ['sessionService'])
session.controller('sessionCtrl', function($scope, $http, $window, sessionServices) {
$scope.session = {};
$scope.saveSession = function() {
$scope.session.sessionNo = 1;
$scope.session.coach = "mmmm";
$scope.session.modules = "wokr place";
//console.log(user);
$http.post("/session/survey", $scope.session).success(function(data, status) {
$window.location.href = '/';
console.log("Sucessfully getting added bio" + JSON.stringify(data));
})
};
});
That's because .success()
really isn't a function. 那是因为.success()
确实不是一个函数。 As the documentation explains , a promise is returned by $http.post()
which you can chain with .then()
正如文档所解释的那样 , $http.post()
返回一个promise,你可以用它来链接$http.post()
.then()
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
Use promises, "success" function doesn't exists in $http object($http success and error methods are available only in older versions of Angular 1.x, but they've removed in Angular 1.6): 使用promises,$ http对象中不存在“success”函数($ http成功和错误方法仅在Angular 1.x的旧版本中可用,但它们已在Angular 1.6中删除):
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
More in official documentation https://docs.angularjs.org/api/ng/service/ $http 更多官方文档https://docs.angularjs.org/api/ng/service/ $ http
It's because you're using $http.post().success
. 这是因为你正在使用$http.post().success
。
Try; 尝试;
$scope.saveSession = function() {
$http.post("/session/survey", $scope.session).then(function(data, status) {
$window.location.href = '/';
console.log("Sucessfully getting data" + JSON.stringify(data));
})
}
We use .then to return a "promise" from the $http service. 我们使用.then从$ http服务返回“promise”。
Hope it helps! 希望能帮助到你!
I just dealt with a similar issue with versions 1.7.2 and 1.7.4. 我刚刚处理了版本1.7.2和1.7.4的类似问题。 It's not exactly the same issue because I was never using .success but I'm posting here because this post comes up first when searching. 这不是完全相同的问题,因为我从未使用过.success但我在这里发帖是因为这篇文章在搜索时首先出现。
When using the shortcut version $http.post(/api/endpoint/', data) I would get: 当使用快捷版本$ http.post(/ api / endpoint /',data)时,我会得到:
"TypeError: $http.post is not a function" “TypeError:$ http.post不是函数”
And if I used it with the callbacks exactly as it appears in the documentation : 如果我将它与回调一起使用,就像文档中显示的那样:
$http({method: 'POST', url: '/api/endpoint/', data: $scope.newObject}).then(function (response) {
$scope.status = response.status;
$scope.data = response.data;
}, function (response) {
$scope.data = response.data || 'Request failed';
$scope.status = response.status;
});
I was getting 我得到了
"TypeError: $http(...).then is not a function" “TypeError:$ http(...)。那么它不是一个函数”
In this case the problem was that I had both $resource and $http in the same controller. 在这种情况下,问题是我在同一个控制器中同时拥有$ resource和$ http。 Not sure if this is the intended behavior but removing $resource suddenly made $http work again. 不确定这是否是预期的行为,但删除$资源突然使$ http再次工作。
Hopefully this helps someone else 希望这有助于其他人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.