简体   繁体   English

React-Native 应用程序中的错误。 错误是`[AxiosError: Network Error]`

[英]Error in React-Native app. the error is `[AxiosError: Network Error]`

I can fetch data easily in a React app.But when I am going to fetch data in a React-Native app.我可以在React应用程序中轻松获取数据。但是当我要在React-Native应用程序中获取数据时。 It shows [AxiosError: Network Error] error.它显示[AxiosError: Network Error]错误。

axios
      .get(`http://localhost:8000/api/products`)
      .then(res => setProducts(res?.data))
      .catch(error => console.log(error));
  }, []);

I assume you are trying to make the calls from an emulator?我假设您正在尝试从模拟器拨打电话?

If yes, localhost is not going to work.如果是,则localhost将无法正常工作。 Depending on whether you are running ios or android you have to call根据您运行的是 ios 还是 android 您必须调用

10.0.2.2 for Android or 127.0.0.1 for ios. 10.0.2.2用于 Android 或127.0.0.1用于 ios。 (I know 127.0.0.1 should be the same as localhost, but it seems that is not the case in the ios emulator. Would be interesting to know why exactly that is the case) (我知道 127.0.0.1 应该与 localhost 相同,但在 ios 模拟器中似乎并非如此。知道为什么会这样会很有趣)

A little more infos here更多信息在这里

Hey you just cant connect directly localhost with your react-native develpoment since it already has a port assigned on which it runs.嘿,您不能直接将 localhost 与您的 react-native 开发人员连接,因为它已经分配了一个运行它的端口。

You can check this article which explains greatly about how to cinfugyre.您可以查看这篇文章,该文章详细解释了如何进行 cinfugyre。 React Native Android Fetch failing on connection to local API React Native Android 在连接到本地 API 时获取失败

Basically you have to map your port to via ADB基本上你必须通过 ADB 将你的端口 map

this is the exact quote:这是确切的报价:

You are not able to access your local development server because that port hasn't been forwarded by ADB yet.您无法访问本地开发服务器,因为 ADB 尚未转发该端口。 When you run react-native run-android, React Native maps the port 8081 with your mobile device on USB.当你运行 react-native run-android 时,React Native 将端口 8081 映射到 USB 上的移动设备。 When you disconnect your USB you won't be able to refresh or hot reload your code anymore.当您断开 USB 时,您将无法再刷新或热重新加载您的代码。 So in this situation you can do 2 things, either map your local server port just like React Native does or use your local IP address所以在这种情况下你可以做两件事,或者 map 你的本地服务器端口,就像 React Native 一样,或者使用你的本地 IP 地址

Do lemme know in case of any concerns如果有任何问题,请让我知道

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

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