Sky*_*ker 2 javascript http angularjs
我正在尝试将表单数据提交给我创建的API端点.我已经在PostMan中测试了它,并且API功能很好,我可以成功获取数据.但是当将该API端点连接到角度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));
})
}
Run Code Online (Sandbox Code Playgroud)
注意:
$scope.session是使用ng-model标记填充的对象.例如:
<input type="text" ng-model="session.title">
Run Code Online (Sandbox Code Playgroud)
编辑(控制器代码):
// 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));
})
};
});
Run Code Online (Sandbox Code Playgroud)
chr*_*con 10
那是因为.success()真的不是一个功能.正如文档所解释的那样$http.post(),您可以链接一个承诺.then()
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
Run Code Online (Sandbox Code Playgroud)
使用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.
});
Run Code Online (Sandbox Code Playgroud)
更多官方文档https://docs.angularjs.org/api/ng/service/ $ http
| 归档时间: |
|
| 查看次数: |
12038 次 |
| 最近记录: |