jQuery 使用 formdata 发布数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10937258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:59:31  来源:igfitidea点击:

Posting array using formdata

jqueryarraysimagehtmlpost

提问by Rainer Sauerstoff

I am using the new HTML5 FormData-Object to post some values and an image via Ajax. It works fine so far. Now, I want to post an array using this object, but all I′ve got on server-side is "[object - object]". How can I post an array with formdata?

我正在使用新的 HTML5 FormData-Object 通过 Ajax 发布一些值和图像。到目前为止它运行良好。现在,我想使用这个对象发布一个数组,但我在服务器端得到的只是“[object - object]”。如何发布带有 formdata 的数组?

What I′ve got so far

到目前为止我所得到的

var formData=new FormData();
formData.append('text', $('#text').attr('value'));
formData.append('headline',$('#headline').attr('value'));
formData.append('myarray',{key1: 'bla', key2: 'blubb'});

The last line doesn′t work. I send the request with this code

最后一行不起作用。我用这个代码发送请求

                 $.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')
                        }
                    });

Thanks in advance.

提前致谢。

采纳答案by Bill

From your syntax, you appear to be trying to pass an object, not an array. I don't think you can pass objects through HTML form.

从你的语法来看,你似乎试图传递一个对象,而不是一个数组。我认为您不能通过 HTML 表单传递对象。

{ key1 : value1 , key2 : value2 }

vs

对比

[ value1, value2 ]

This is a handy reference to general JS syntax

这是对通用JS 语法的方便参考

回答by Matt Stapleton

Using .append()on each element of the associative array might produce the results you're expecting.

.append()在关联数组的每个元素上使用可能会产生您期望的结果。

In place of this line:

代替这一行:

formData.append('myarray',{key1: 'bla', key2: 'blubb'});

You might try the following:

您可以尝试以下操作:

var myarray = {key1: 'bla', key2: 'blubb'};

jQuery.each(myarray, function(key, value) {
    formData.append('myarray['+key+']', value);
});

回答by Rainer Sauerstoff

Thanks. I now came up with this solution:

谢谢。我现在想出了这个解决方案:

                for (i = 0; i < social_networks.length; i++) {
                    formData.append("myarray["+i+"][mykey]",arr[i]['mykey']);
                    formData.append("myarray["+i+"][mykey2]",arr[i]['mykey2']);
                }

回答by codegrid

Try this. It worked for me.

尝试这个。它对我有用。

var files = $scope.myFile;
        var fd = new FormData();
        fd.append("file", files[0]);
        fd.append("assignment", JSON.stringify({ classAssignment: $scope.editItem }));

回答by Shailendra bind

my arrary list is like this

我的数组是这样的

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"}]

This code used to format data like key-value pairs

此代码用于格式化键值对等数据

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);
            });

This is output of serialized Data

这是序列化数据的输出

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"}

Its working for me

它为我工作

But in other post i seen data is pass like

但在其他帖子中,我看到数据是通过的

 {name: "billlist[0][Type]", value: "1"}

Way it not working for me

它对我不起作用