![](/img/trans.png)
[英]React-native Android emulator - POST fails with TypeError: Network request failed
[英]TypeError: Network Request Failed when trying to hit local API from android emulator. React-Native
我到处都看到了这个问题的答案,但似乎没有一个对我有用。
我正在尝试从我的 android 模拟器中进行反应原生获取,并点击在 https://localhost:5002/v1/users 上运行的本地 API。
这就是我的 fetch 现在的样子:
var response: Response = await fetch(`https://10.0.2.2:5002/v1/user/${initialContext.userId}`, {
method: "GET",
headers: {
"Authorization": `Bearer ${initialContext.accessToken}`
}
});
这是我得到的错误:
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)
如您所见,我已经尝试使用 10.0.2.2(与模拟器等效的本地主机)运行它,并且还尝试了计算机的 IP 地址。 我也尝试过运行 adb reverse 和其他端口转发方法。
对我来说最难理解的部分是,如果我在模拟器上的 chrome 上使用 go 到https://10.0.2.2:5002 ,我就可以访问我的本地 ZDB974Z42387108CA8146 我还可以从该 fetch 语句中找到云中的 API,而不是在本地主机上运行任何东西。
任何想法,将不胜感激。
实际上,使用ngrok很容易工作,它在您的模拟器和 localhost(网络隧道)之间起作用。 它为您提供一个域并将您的所有请求转发到 localhost。 您可以查看此文档https://ngrok.com/docs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.