简体   繁体   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 . 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.

相关问题 在 Android 物理设备上反应本机 axios 后请求网络错误 - React native axios post request network error on Android physical device Axios POST 请求在 React Native 中将图像添加到 FormData 结构时给出“网络错误” - Axios POST request gives a “Network Error” when adding image to FormData structure in React Native 使用 axios 访问在 localhost 上运行的 api 时,React Native 应用程序 GET 请求导致网络错误 - React Native app GET request resulting in network error when using axios to reach for api running on localhost 使用 Axios 或在 android 中获取时反应本机抛出网络错误 - React Native throw Network Error when using Axios or Fetch in android 错误:网络错误 Axios - Image React Native - Error: Network Error Axios - Image React Native Axios React Native 网络错误 Android Expo - Axios Network Error in React Native with Expo in Android 在 React Native 中使用 axios 时出现网络错误 - Network Error when use axios in React Native React-native - Axios GET请求在Android模拟器中引发网络错误 - React-native - Axios GET request throws network error in Android emulator 错误:在 React Native 中使用 axios 的网络错误,仅在 iOS 和 Android 中出现 - Error: Network Error using axios in React Native, issue in only in iOS and Android 无法在 axios 上发出 POST 请求(React-Native) - Unable to make POST request on axios (React-Native)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM