[英]POST form data using AngularJS
I'm trying to use AngularJS to POST a form to an API I'm developing. 我正在尝试使用AngularJS将表单发布到正在开发的API中。
The form has novalidate
, name="addMatchForm"
, and ng-submit="submit(addMatchForm)"
. 该表单具有
novalidate
, name="addMatchForm"
和ng-submit="submit(addMatchForm)"
。 The addMatchForm
is as followed: addMatchForm
如下所示:
app.controller('addMatchController', ['$scope', 'players', 'matches', function($scope, players, matches) {
...
$scope.submit = function(form) {
form.submitted = true;
// Make sure we don't submit the form if it's invalid
if ( form.$invalid ) return;
matches.add(form).success(function(data) {
console.log('post result:', data);
})
}
}]);
app.factory('matches', function($http) {
return {
add: function(data) {
return $http.post('/matches', data);
}
}
});
But the weird thing is that each and every input's value becomes an empty object at that point. 但是奇怪的是,每个输入的值都在那个时候变成一个空对象。 This is the Form Data from Chrome Developer Tools:
这是来自Chrome开发者工具的表单数据:
{"a_score":{},"b_score":{},"day":{},"month":{},"year":{},"a_player1":{},"a_player2":{},"b_player1":{},"b_player2":{},"submitted":true}:
I did add the content-type
part already. 我确实已经添加了
content-type
部分。
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
Any ideas? 有任何想法吗?
Try to use $http like this: 尝试使用$ http这样:
return $http({
method: 'POST',
url: '/matches',
data: $.param(data),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.