我正在使用新的HTML5 FormData-Object通过Ajax发布一些值和图像。它到目前为止工作正常。现在,我想使用这个对象发布一个数组,但我在服务器端的所有内容都是“[object - object]”。如何使用formdata发布数组?
到目前为止我得到了什么
var formData=new FormData();
formData.append('text', $('#text').attr('value'));
formData.append('headline',$('#headline').attr('value'));
formData.append('myarray',{key1: 'bla', key2: 'blubb'});
最后一行不起作用。我发送带有此代码的请求
$.ajax({
url: 'xyz',
data: formData,
type: 'POST',
processData: false,
contentType: false,
success: function(data) {
var decoded=$.parseJSON(data);
displaySuccess('Success', decoded.message);
},error: function(data){
var decoded=$.parseJSON(data);
displayError('Error', decoded.message);
},complete: function(data){
$('#cursor').hide();
$("#submitbutton").removeAttr('disabled')
}
});
提前致谢。
答案 0 :(得分:16)
在关联数组的每个元素上使用.append()
可能会产生您期望的结果。
取代这一行:
formData.append('myarray',{key1: 'bla', key2: 'blubb'});
您可以尝试以下方法:
var myarray = {key1: 'bla', key2: 'blubb'};
jQuery.each(myarray, function(key, value) {
formData.append('myarray['+key+']', value);
});
答案 1 :(得分:5)
感谢。我现在提出了这个解决方案:
for (i = 0; i < social_networks.length; i++) {
formData.append("myarray["+i+"][mykey]",arr[i]['mykey']);
formData.append("myarray["+i+"][mykey2]",arr[i]['mykey2']);
}
答案 2 :(得分:3)
根据您的语法,您似乎尝试传递对象,而不是数组。我认为你不能通过HTML表单传递对象。
{ key1 : value1 , key2 : value2 }
<强> VS 强>
[ value1, value2 ]
这是对一般JS syntax
的方便参考答案 3 :(得分:2)
试试这个。它对我有用。
var files = $scope.myFile;
var fd = new FormData();
fd.append("file", files[0]);
fd.append("assignment", JSON.stringify({ classAssignment: $scope.editItem }));
答案 4 :(得分:0)
我的服装清单是这样的
billlists = [{ Color: "White"Quantity: "1"Shape: "2.0"SizeNo1: "1"SizeNo2: "1"StoneColorCost: "20000"StoneColorWT: "0.006"Type: "1"__proto__: Object1: Color: "White"Quantity: "1"Shape: "2.0"SizeNo1: "0.5"SizeNo2: "0.7"StoneColorCost: "6"StoneColorWT: "0.005"Type: "1"},
{ Color: "White"Quantity: "1"Shape: "2.0"SizeNo1: "1"SizeNo2: "1"StoneColorCost: "20000"StoneColorWT: "0.006"Type: "1"__proto__: Object1: Color: "White"Quantity: "1"Shape: "2.0"SizeNo1: "0.5"SizeNo2: "0.7"StoneColorCost: "6"StoneColorWT: "0.005"Type: "1"}]
此代码用于格式化键/值对之类的数据
function serializeData(name, arr)
{
var a = [];
for (var i = 0; i < arr.length; i++)
{
for (var key in arr[i])
{
a.push({ name: name + '[' + i + '].' + key + '', value: arr[i][key] });
}
}
return a;
}
var mydata = serializeData('billlist', billlists);
$.each(mydata, function (key, input) {
fd.append(input.name, input.value);
});
这是序列化数据的输出
0: {name: "billlist[0].Type", value: "1"}
1: {name: "billlist[0].Color", value: "White"}
2: {name: "billlist[0].Shape", value: "2.0"}
3: {name: "billlist[0].SizeNo1", value: "1"}
4: {name: "billlist[0].SizeNo2", value: "1"}
5: {name: "billlist[0].Quantity", value: "1"}
6: {name: "billlist[0].StoneColorWT", value: "0.006"}
7: {name: "billlist[0].StoneColorCost", value: "20000"}
8: {name: "billlist[1].Type", value: "1"}
9: {name: "billlist[1].Color", value: "White"}
10: {name: "billlist[1].Shape", value: "2.0"}
11: {name: "billlist[1].SizeNo1", value: "0.5"}
12: {name: "billlist[1].SizeNo2", value: "0.7"}
13: {name: "billlist[1].Quantity", value: "1"}
14: {name: "billlist[1].StoneColorWT", value: "0.005"}
15: {name: "billlist[1].StoneColorCost", value: "6"}
为我工作
但是在其他帖子中,我看到数据像一样通过
{name: "billlist[0][Type]", value: "1"}
对我不起作用