因此,我将一个带有axios的formdata对象发布到node.js服务器上。在iOS上,一切都很完美,数据被发布,图像被上传。但是在android上我得到了这个错误
[AxiosError: Network Error]
这是我的axios调用
const handleSubmit = async (listing, { resetForm }) => {
const data = new FormData();
listing.images.forEach((image, index) =>
data.append("images", {
name: `product${Math.floor(Math.random() * 1000)}.`,
uri: image,
})
);
const res = await axios
.post("http://192.168.43.8:5000/products/addProduct", data, {
headers: {
"Content-Type": "multipart/form-data",
},
//tried adding this but didn't work out
transformRequest: (data) => data,
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
// handle error
});
}
}
请注意,在iOS上,它的工作没有任何问题。下面是使用react本机调试器时出错的屏幕截图
发布于 2022-06-29 16:34:52
如果您使用安卓仿真器,您需要将您的ip更改为10.0.2.2
发布于 2022-06-29 16:42:08
默认情况下,从android 9版本开始,http调用将被阻止,您要么需要发出api调用HTTPS,要么需要在清单文件中显式地允许连接到此IP。请参考这个线程。How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?
发布于 2022-11-21 12:16:44
对我来说,我的模拟器有axios错误,而ios模拟器没有任何错误。我的模拟器的问题是它无法连接到Internet。
因此,我为我的mac添加了google服务器8.8.8.8 (您可以添加任何DNS服务器),它起了作用。
https://stackoverflow.com/questions/72804863
复制相似问题