【发布时间】:2018-05-28 14:48:25
【问题描述】:
我正在向“Cloudinary”服务器发出 POST 请求,以在 react-native 中使用 axios 上传图像并发送表单数据。整个过程在 iOS 上运行良好,但在 android 上出现“网络错误”。
我正在使用 axios 0.18.0 并且从过去 3 天开始一直坚持使用此版本。有人请帮帮我。
这个问题很多人都遇到过,但目前还没有解决办法。
var photo = {
uri: image.sourceURL,
type: image.mime,
name: image.filename,
};
var formData = new FormData();
formData.append('file',photo);
formData.append('upload_preset','abcde');
axios({
url:'https://api.cloudinary.com/v1_1/abcde/upload',
method:'POST',
headers:{
'Content-Type':'application/x-www-form-urlencoded'
},
data:formData
}).then(function(response){
}).catch((error) =>{
//Network error comes in
});
确切的控制台错误是:
Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:87)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:567)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:397)
at XMLHttpRequest.js:503
at RCTDeviceEventEmitter.emit (EventEmitter.js:179)
at MessageQueue.__callFunction (MessageQueue.js:351)
at MessageQueue.js:116
at MessageQueue.__guardSafe (MessageQueue.js:314)
有人能指出我正确的方向吗,因为我从过去 3 天就被困在这里了。 P.S:相同的代码在 iOS 上运行良好。
【问题讨论】:
-
有同样的问题,经过一段时间后能够发现的是,在发布表单数据时,由于某种原因,在本机反应中,正文保持为空,请参阅 Axios 中打开的错误 - link。如果正文为空,Axios 将在幕后删除
Content-Type标头
标签: android react-native networking axios