简体   繁体   English

使用 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

I'm working on an Android App that's running on an Android Emulator and it's supposed to use Axios to get data from the API that's running on my localhost:3333, the problem is that when it sends the GET request, I get a Network Error as if it can't reach anything by using the baseURL "http://localhost:3333". I'm working on an Android App that's running on an Android Emulator and it's supposed to use Axios to get data from the API that's running on my localhost:3333, the problem is that when it sends the GET request, I get a Network Error好像它无法通过使用 baseURL "http://localhost:3333" 访问任何东西。

I know that because it's a VM, I am supposed to use adb reverse tcp:3333 tcp:3333 to connect it's ports to the host machine to keep using "localhost";我知道因为它是一个虚拟机,所以我应该使用 adb reverse tcp:3333 tcp:3333 将其端口连接到主机以继续使用“localhost”;

I've already tried using the Android Studio IP "http://10.0.2.2:3333" without the adb reverse;我已经尝试在没有 adb reverse 的情况下使用 Android Studio IP "http://10.0.2.2:3333";

I've already tried using my local ip as the baseURL;我已经尝试使用本地 ip 作为 baseURL;

But the problem continues...但问题还在继续……

Instead of posting snippets, I'm sharing the projects, as they're very simple: APP https://github.com/March0514s/react-native react-native run-android我没有发布片段,而是分享项目,因为它们非常简单:APP https://github.com/March0514s/react-native react-native run-android

API https://github.com/March0514s/backend yarn dev API https://github.com/March0514s/backend yarn dev

Obs.: I'm not sure if it's necessary to reproduce the error, but send the following POST after the server is running: curl -X POST -H "Content-Type: application/json" -d '{"title": "project", "name": "your name"} http://localhost:3333/projects观察:我不确定是否有必要重现错误,但在服务器运行后发送以下 POST: curl -X POST -H "Content-Type: application/json" -d '{"title": “项目”,“名称”:“你的名字”} http://localhost:3333/projects

Thanks in advance提前致谢

The problem was being caused by the back-end that was configured to use the port 3333 that was being used for something else, and that lead me to the weird behavior of being capable of getting the response by http://localhost:3333 but not from http://127.0.0.1:3333 on the host machine.问题是由配置为使用用于其他用途的端口 3333 的后端引起的,这导致我出现了能够通过 http://localhost:3333 获得响应的奇怪行为,但是不是来自主机上的http://127.0.0.1:3333 Using another port fixed that problem and the techniques used for loopback on the VM worked as they should.使用另一个端口解决了这个问题,并且在 VM 上用于环回的技术可以正常工作。 Thanks anyway for at least reading my problem, it's always good to have a community to rely on when those weird stuff happens...无论如何,感谢您至少阅读我的问题,当那些奇怪的事情发生时,有一个社区可以依赖总是好的......

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

相关问题 使用 Axios 或在 android 中获取时反应本机抛出网络错误 - React Native throw Network Error when using Axios or Fetch 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 Axios POST 请求在 React Native 中将图像添加到 FormData 结构时给出“网络错误” - Axios POST request gives a “Network Error” when adding image to FormData structure in React Native 在 Android 物理设备上反应本机 axios 后请求网络错误 - React native axios post request network error on Android physical device 使用 Axios 响应本机 POST 请求:即使在使用服务器的 IP 后出现网络错误 - React Native POST request with Axios: Network Error even after using Server's IP 错误:网络错误 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 FlatList 不渲染 Axios API 请求 - React Native FlatList is not rendring an Axios API request 运行本机应用程序时出错 - Getting an error when running react native app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM