Reputation: 175
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);
Upvotes: 2
Views: 5232
Reputation: 2440
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 compatible 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 comma-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.
Upvotes: 8