[英]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.