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
Add a ment  | 

1 Answer 1

Reset to default 6

You can use JSON.stringify(data.customer) before appending to FormData.

本文标签: javascriptReactHow to append an object as a field to FormDataStack Overflow