1

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

张旭超
1.4k 声望222 粉丝

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