[英]React Native POST request with Axios: Network Error even after using Server's IP
I have my local REST API served in http://localhost:4000
however, any axios request from React Native is not working, it always yields to Network Error
but requests works fine in Postman
. I have my local REST API served in
http://localhost:4000
however, any axios request from React Native is not working, it always yields to Network Error
but requests works fine in Postman
. I am running the app on my physical android device
through a cable.我正在通过电缆在我的
physical android device
上运行该应用程序。
I've tried using the IP address of the server's machine being run which I got by running ipconfig
on command line but still no luck我已经尝试使用正在运行的服务器机器的 IP 地址,这是我通过在命令行上运行
ipconfig
获得的,但仍然没有运气
// const APIUrl = 'http://localhost:4000/api/v1';
const APIUrl = 'http://192.168.12.4:4000/api/v1';
export const axiosClient = axios.create({ baseURL: APIUrl });
export const login = async (username, password) => {
try {
const res = await axiosClient({
url: '/login',
method: 'POST',
data: { username, password },
headers: { 'Content-Type': 'application/json' }
});
return res.data;
} catch (err) {
console.log(err);
}
}
I execute the app by running these commands我通过运行这些命令来执行应用程序
$ adb -s <mydevice> reverse tcp:8081 tcp:8081
$ npx react-native run-android
Found out what has caused the problem.找出导致问题的原因。 I didn't know that the emulator was connected to other wifi network, they should be on the same network.
我不知道模拟器连接到其他wifi网络,它们应该在同一个网络上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.