admin管理员组

文章数量:1399957

I've three arrays and I've to send them into formdata through Axios.the key for the array is record[].but I've three arrays and I've only one key how can I send three array on the same key record[] here is my code

const params = new FormData();
      params.append("record[]", this.props.addedOrders);
      params.append("record[]", this.props.addedCustomers);
      params.append("record[]", this.props.addedRecovery);

I've three arrays and I've to send them into formdata through Axios.the key for the array is record[].but I've three arrays and I've only one key how can I send three array on the same key record[] here is my code

const params = new FormData();
      params.append("record[]", this.props.addedOrders);
      params.append("record[]", this.props.addedCustomers);
      params.append("record[]", this.props.addedRecovery);
Share Improve this question edited Jul 30, 2020 at 4:22 Zaid Qureshi asked Jul 29, 2020 at 14:55 Zaid QureshiZaid Qureshi 1751 gold badge3 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Multiple values can be added to the same key with FormData. From the doc:

As with regular form data, you can append multiple values with the same name. For example (and being patible with PHP's naming conventions by adding [] to the name):

Here's an example:

const params = new FormData();

params.append('record[]', 1);
params.append('record[]', 2);
params.append('record[]', 3);

console.log(params.getAll('record[]'));

Since your question asks about appending arrays to FormData, it's worth pointing out that FormData.append works with strings and blobs, and other types will be converted to strings.

If the sent value is different than String or Blob it will be automatically converted to String.

So, if you try to append an array, it will be converted to a ma-delimited list by default. For arrays of objects that will serialize to something like [object Object]. You could alternatively marshall each array to a string by using e.g. JSON.stringify, then convert the data back on the server side, but that's an implementation choice.

const params = new FormData();

params.append('record[]', JSON.stringify([{name: 'joe'}, {name: 'bob'}]));
params.append('record[]', JSON.stringify([{name: 'rand'}, {name: 'smith'}]));

console.log(params.getAll('record[]'));

All that said, I would say your issue lies elsewhere.

本文标签: javascriptHow to append arrays into FormData in react nativeStack Overflow