admin管理员组文章数量:1391947
I am using React, and am trying to send data back to my backend (DRF) using FormData to properly store the data. However, I am facing some issues with appending objects as fields into FormData, as it would be converted to [object, Object]. Is there any way to overe this?
Here is my code for reference
my data before it is passed into formdata
{ quotation: "22222.00",
customer: {customer_name: 'Customer A', address: 'Address B', number: '123456789'}
}
how i pass the data into formdata
let formData = new FormData();
formData.append('quotation', data.quotation);
formData.append('customer', data.customer);
after appending the data into formdata, when logging the formdata, this is what the customer field bees
customer: [object Object]
this is the data received by the backend
{'quotation': '22222.00', 'customer': '[object Object]' }
Do guide me along, thanks all!
I am using React, and am trying to send data back to my backend (DRF) using FormData to properly store the data. However, I am facing some issues with appending objects as fields into FormData, as it would be converted to [object, Object]. Is there any way to overe this?
Here is my code for reference
my data before it is passed into formdata
{ quotation: "22222.00",
customer: {customer_name: 'Customer A', address: 'Address B', number: '123456789'}
}
how i pass the data into formdata
let formData = new FormData();
formData.append('quotation', data.quotation);
formData.append('customer', data.customer);
after appending the data into formdata, when logging the formdata, this is what the customer field bees
customer: [object Object]
this is the data received by the backend
{'quotation': '22222.00', 'customer': '[object Object]' }
Do guide me along, thanks all!
Share Improve this question asked Jul 12, 2020 at 10:23 jasonjason 6121 gold badge12 silver badges27 bronze badges 2-
5
You can stringify the object before appending to the formData. Like -
formData.append('customer', JSON.stringify(data.customer))
– Sajeeb Ahamed Commented Jul 12, 2020 at 10:25 - 1 What happens if you use JSON.stringify on data.customer instead ? – Axnyff Commented Jul 12, 2020 at 10:26
1 Answer
Reset to default 6You can use JSON.stringify(data.customer)
before appending to FormData.
本文标签: javascriptReactHow to append an object as a field to FormDataStack Overflow
版权声明:本文标题:javascript - React - How to append an object as a field to FormData - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744755310a2623426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论