加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何用React-Native Post Form 表单

发布时间:2020-12-15 04:41:06 所属栏目:百科 来源:网络整理
导读:今天使用react native 发送请求的时候,发现使用 fetch( 'https://mywebsite.com/endpoint/' ,{ method: 'POST' ,headers: { },body: JSON. stringify({ firstParam: 'yourValue' ,secondParam: 'yourOtherValue' ,}) }) 默认发送’Content-Type’: ‘applic

今天使用react native 发送请求的时候,发现使用

fetch('https://mywebsite.com/endpoint/',{ method: 'POST',headers: { },body: JSON.stringify({ firstParam: 'yourValue',secondParam: 'yourOtherValue',}) })

默认发送’Content-Type’: ‘application/json’ 的请求,但是现在想发送Form表单,该怎么写尼?查阅官方文档
无解,各种stackoverflow还是没有类似的答案,每当stackoverflow上都没有答案的时候,我就准备看源码了,这时候,突然想到立成同学的一句话,github的issues上面比文档还可靠,抱着试试看的态度,翻了一下issue,原来github的issue还有内部搜索功能,真方便。功夫不负有心人,在#5308
这里找到了答案:

let formData = new FormData();
formData.append('image',{uri: image.uri,type: 'image/jpg',name: 'image.jpg'});
formData.append('description',String(data.description));

let options = {};
options.headers['Content-Type'] = 'multipart/form-data; boundary=6ff46e0b6b5148d984f148b6542e5a5d';
options.body = formData;
return fetch(uri,options).then((response) => { .... });

只要把body从Json换成FromData就可以了,解决完问题然后下班的感觉真好。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读