Reactjs 使用axios/fetch在react native中发送formData
从过去几天开始,我尝试使用axios/fetch发送带有所需头的formData,但响应是网络错误 请大家分享一下如何在react native中工作的代码片段 谢谢 代码如下: 试试这个:Reactjs 使用axios/fetch在react native中发送formData,reactjs,react-native,axios,fetch,Reactjs,React Native,Axios,Fetch,从过去几天开始,我尝试使用axios/fetch发送带有所需头的formData,但响应是网络错误 请大家分享一下如何在react native中工作的代码片段 谢谢 代码如下: 试试这个: var formData = new FormData(); // make sure it is formData not formdata axios({ url: url, method: 'POST', headers: {
var formData = new FormData(); // make sure it is formData not formdata
axios({
url: url,
method: 'POST',
headers: {
"Content-Type": 'multipart/form-data',
},
formData
})
试着这样,
var formdata = new FormData();
formdata.append("file", file, this.state.fileName);
formdata.append("folderName", this.state.folderName);
formdata.append("userName", "myname@gmail.com");
formdata.append("documents", documents);
axios({
method: 'post',
url: 'url',
data: formdata,
headers: {'Content-Type': 'multipart/form-data' }
})
.then(function (response) {
//handle success
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});
共享您尝试的代码,以便我们可以尝试提供帮助。我们需要更多信息,如您收到的代码和错误消息。@TravisJames我已添加了代码,请查看。@SathishkumarRakkiasamy我已包含我的代码供参考,每次我调用api时,它都会抛出网络错误。
formdata
应该是formdata对象。您应该在向其添加数据之前声明它:const formdata=new formdata();formdata.append(…)
我尝试了上面的代码,但幸运的是它抛出了网络错误。那么你应该将它包含在你的代码片段中,因为它是你问题的重要部分,而粘贴它时我错过了它。一点也不担心!有没有其他方法来实现它?检查您的URL,如果您遇到网络错误,请确保它是正确的
var formdata = new FormData();
formdata.append("file", file, this.state.fileName);
formdata.append("folderName", this.state.folderName);
formdata.append("userName", "myname@gmail.com");
formdata.append("documents", documents);
axios({
method: 'post',
url: 'url',
data: formdata,
headers: {'Content-Type': 'multipart/form-data' }
})
.then(function (response) {
//handle success
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});