React Native XHR multipart/form-data send data as [object Object]

我们两清 提交于 2021-02-19 06:10:55

问题


I'm trying to send a multipart/form-data from React Native (Running on Simulator) to backend server using XHR with the following code

let xhr = new XMLHttpRequest();
xhr.open('POST', 'http://....url....');
let formdata = new FormData();
formdata.append('title','This is awesome');
xhr.send(formdata);

However in console log the Request Payload are shown as [object Object]

So I decided to take the same code and put it in an HTML file and run from Chrome browser and Request Payload from console log is showing as below which I was expecting it from React Native

------WebKitFormBoundaryGXwudBdBkJzBnPug
Content-Disposition: form-data; name="title"

This is awesome
------WebKitFormBoundaryGXwudBdBkJzBnPug--

回答1:


I had the same issue and after a day of research I found that we have to apply a polyfill, because React Native environment differs from what you can find in browser or Node.js.

Put this line in your main index file, and you should be good:

global.FormData = global.originalFormData ? global.originalFormData : global.FormData;

for TypeScript define global first:

declare const global: any;


来源:https://stackoverflow.com/questions/44520812/react-native-xhr-multipart-form-data-send-data-as-object-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!