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
1 Answer
Reset to default 8Multiple 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
版权声明:本文标题:javascript - How to append arrays into FormData in react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744160265a2593304.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论