繁体   English   中英

使用 Axios 响应本机 POST 请求:即使在使用服务器的 IP 后出现网络错误

[英]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 . 我正在通过电缆在我的physical android device上运行该应用程序。

我已经尝试使用正在运行的服务器机器的 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);
   }
}

我通过运行这些命令来执行应用程序

$ adb -s <mydevice> reverse tcp:8081 tcp:8081

$ npx react-native run-android

找出导致问题的原因。 我不知道模拟器连接到其他wifi网络,它们应该在同一个网络上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM