我尝试发布表单数据。但它没有用。 以下是我的代码:
var app = angular.module('myApp', []);
// Controller function and passing $http service and $scope var.
app.controller('myCtrl', function($scope, $http) {
// create a blank object to handle form data.
$scope.user = {};
// calling our submit function.
$scope.submitForm = function() {
// Posting data to file
$http({
method : 'POST',
url : '/tokken/d/',
data : $scope.user, //forms user object
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
if (data.errors) {
// Showing errors.
$scope.errorName = data.errors.name;
$scope.erroPassword = data.errors.password;
} else {
$scope.message = data.message;
}
});
};
});
有人可以帮我吗?
答案 0 :(得分:0)
$scope.user is a JSON object not form-data
因此您需要将内容类型设置为
Content-Type: application/json
你需要在ng submit
上调用该函数在服务器中,您需要将json解析为数据
答案 1 :(得分:0)
尝试使用$http.post()
:
var app = angular.module('myApp', []);
// Controller function and passing $http service and $scope var.
app.controller('myCtrl', function($scope, $http) {
// create a blank object to handle form data.
$scope.user = {};
// calling our submit function.
$scope.submitForm = function() {
// Posting data to file
$http.post('/tokken/d/', $scope.user).then(function (data) {
if (data.errors) {
// Showing errors.
$scope.errorName = data.errors.name;
$scope.erroPassword = data.errors.password;
} else {
$scope.message = data.message;
}
});
};
});