我正在尝试将表单数据提交到我创建的 API 端点。我已经在 PostMan 中测试过了,API 运行良好,我可以成功获取数据。但是在将该 API 端点连接到 angular js 中的函数时,出现以下错误。
这是我的代码:
$scope.saveSession = function() {
$http.post("/session/survey", $scope.session).success(function(data, status) {
$window.location.href = '/';
console.log("Sucessfully getting data" + JSON.stringify(data));
})
}
笔记:
$scope.session
是一个使用 ng-model
标签填充的对象。例如:
<input type="text" ng-model="session.title">
编辑(控制器代码):
// 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));
})
};
});
原文由 Skywalker 发布,翻译遵循 CC BY-SA 4.0 许可协议
那是因为
.success()
真的不是一个函数。正如 文档所解释 的那样,$http.post()
返回一个承诺,您可以将其与.then()
链接起来