从 Angular $http POST 传递数据数组 [英] Pass array of data from Angular $http POST
问题描述
我需要使用 Nancy 框架将对象数组从我的 Angular 应用程序传递到 .Net Web 服务.
I need to pass an array of object from my Angular application to a .Net web service with Nancy framework.
我试过了:
function TestCtrl($scope, $http){
$scope.postTest = function(){
var data = [obj1, obj2, obj3];
$http({
url: 'myURL',
method: "POST",
data: data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).success(function(data){
alert("done");
});
}
}
但是服务器发送 500 内部服务器错误.
我不知道为什么它不起作用.我不是网络服务专家,但我认为这是一个序列化问题.
But server send 500 Internal server error.
I don't know why it doesn't work. I'm not an expert web service expert but I think it's a serialization problem.
有人可以帮我吗?
推荐答案
根据 这篇文章,你说得对,这是关于序列化的.Angular 不会自动为你序列化数据,你需要解析数据在发送之前:
According to this post, you're right, this is about serialization. Angular doesn't automatic serialize the data for you, you need to parse the data before sending it:
...
$http({
url: 'myURL',
method: "POST",
data: $.param(data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})...
如果您不使用 jQuery,则需要滚动自己的 $.parse
.有一个代码片段,或者您可以适配 jQuery 实现.
If you don't use jQuery, you'll need to roll your own $.parse
. There is a snippet here or you could adapt jQuery implementation.
这篇关于从 Angular $http POST 传递数据数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!