9

FormData传数组对象的方法

 3 years ago
source link: https://segmentfault.com/a/1190000040452911
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

FormData传数组对象的方法

前端formData传参,有时候需要传递数组对象,如果直接给变量append一个数组对象后台无法接受。

const arr = [
  {name: 'zxc', age: 10},
  {name: 'ly', age: 20}
]
const fd = new FormData()
fd.append('address', arr)

正确的传参方式如下

arr.forEach((value, index) => {
  fd.append(`address[${index}].name`, value.name)
  fd.append(`address[${index}].age`, value.age)
})

如果是字符串或者数字数组

const arr = [1, 2, 3, 4, 5]

arr.forEach((value, index) => {
  fd.append(`address[${index}]`, value)
})

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK