[英]TypeError: Network Request Failed when trying to hit local API from android emulator. React-Native
I've seen answers to this question everywhere but none of them seem to work for me.我到处都看到了这个问题的答案,但似乎没有一个对我有用。
I'm trying to do a react native fetch from my android emulator and hit my local API running on https://localhost:5002/v1/users.我正在尝试从我的 android 模拟器中进行反应原生获取,并点击在 https://localhost:5002/v1/users 上运行的本地 API。
This is what my fetch looks like right now:这就是我的 fetch 现在的样子:
var response: Response = await fetch(`https://10.0.2.2:5002/v1/user/${initialContext.userId}`, {
method: "GET",
headers: {
"Authorization": `Bearer ${initialContext.accessToken}`
}
});
and this is the error that I'm getting:这是我得到的错误:
Possible Unhandled Promise Rejection (id: 0):
TypeError: Network request failed
TypeError: Network request failed
at C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:26139:20
at C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:30727:21
at _callTimer (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:30643:9)
at Object.callTimers (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:30851:9)
at MessageQueue.__callFunction (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:2781:31)
at C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:2513:17
at MessageQueue.__guard (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:2735:13)
at MessageQueue.callFunctionReturnFlushedQueue (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:2512:14)
at C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\debuggerWorker.js:199:58
at process.<anonymous> (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\debuggerWorker.js:68:9)
As you can see I've tried running it with 10.0.2.2 (the local host equivalent for the emulator) and I've also tried my computer's IP address.如您所见,我已经尝试使用 10.0.2.2(与模拟器等效的本地主机)运行它,并且还尝试了计算机的 IP 地址。 I've also tried running adb reverse and other port forwarding methods.
我也尝试过运行 adb reverse 和其他端口转发方法。
The hardest part for me to wrap my head around is that if I go to https://10.0.2.2:5002 on chrome on my emulator I'm able to hit my local API.对我来说最难理解的部分是,如果我在模拟器上的 chrome 上使用 go 到https://10.0.2.2:5002 ,我就可以访问我的本地 ZDB974Z42387108CA8146 I'm also able to hit an API in the cloud from that fetch statement, just not anything running on local host.
我还可以从该 fetch 语句中找到云中的 API,而不是在本地主机上运行任何东西。
Any ideas would be appreciated.任何想法,将不胜感激。
Actually it's easy to work using ngrok , which acts in between your emulator and localhost(network tunnelling).实际上,使用ngrok很容易工作,它在您的模拟器和 localhost(网络隧道)之间起作用。 It provides you with a domain and forwards all your requests to localhost.
它为您提供一个域并将您的所有请求转发到 localhost。 You can check this documentation https://ngrok.com/docs
您可以查看此文档https://ngrok.com/docs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.