FormData 传递参数有数组类型时,传送的数据格式不对怎么办?

//要传递的数据类型:

//【第一种】:
var arr=[
    {a:"1",b:"s",c:"6"},
    {a:"1",b:"s",c:"6"},
    {a:"1",b:"s",c:"6"}
];
//【第二种】:
var arrs = [
    ["v1","v2","v1"],
    ["v1","v2","v1"],
    ["v1","v2","v1"]
];

//正常直接 formData.append("name", val) 传递,后台接收:

第一种:"arr" => "[object Object],[object Object],[object Object]"
第二种:"arrs" => "v1,v2,v1,v1,v2,v1,v1,v2,v1"

//这些都不是我们想要的,那我们应该怎么处理呢?很简单用JSON.stringify转化一下即可:

formData.append("arr", JSON.stringify(arr));
formData.append("arrs", JSON.stringify(arrs));

//这样就能得到我们想要的数据了

第一种:"arr" => "[{"a":"1","b":"s","c":"6"},{"a":"1","b":"s","c":"6"},{"a":"1","b":"s","c":"6"}]"
第二种:"arrs" => "[["v1","v2","v1"],["v1","v2","v1"],["v1","v2","v1"]]"

 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页