FormData method of passing array objects

张旭超
中文

Front-end formData transfer parameters, sometimes need to transfer array objects, if you directly append an array object to the variable, the back-end cannot accept it.

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

correct way to pass parameters for

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

if it is a string or numeric array

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

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

avatar
张旭超
前端开发工程师

精通 html+div+css jquery, vue, angularjs, angular2, angular4, ionic, ionic2

1.2k 声望
189 粉丝
0 条评论
你知道吗?

avatar
张旭超
前端开发工程师

精通 html+div+css jquery, vue, angularjs, angular2, angular4, ionic, ionic2

1.2k 声望
189 粉丝
宣传栏