android - 如何使用 nativescript 代码连接到 NodeJS 服务器(无 Express)。(“状态代码:未知错误”)
问题描述
我有一个连接到 NodeJS API 的 nativescript 共享代码,它有另一个来源,我已经设置了 CORS 策略,它适用于 web 端,但我在 nativescript 上收到错误。
任何帮助,将不胜感激。
ERROR {
"headers": {
"normalizedNames": {},
"lazyUpdate": null,
"headers": {}
},
"status": 0,
"statusText": "Unknown Error",
"url": "http://localhost:4000/api/users",
"ok": false,
"name": "HttpErrorResponse",
"message": "Http failure response for http://localhost:4000/api/users: 0
Unknown Error",
"error": {
"originalStack": "Error: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:4000\n at new ZoneAwareError (file:///data/data/org.nativescript.ngsample/files/app/vendor.js:82629:33)\n at onRequestComplete (file:///data/data/org.nativescript.ngsample/files/app/vendor.js:101918:34)\n at Object.onComplete (file:///data/data/org.nativescript.ngsample/files/app/vendor.js:101910:13)",
"zoneAwareStack": "Error: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:4000\n at onRequestComplete (file:///data/data/org.nativescript.ngsample/files/app/vendor.js:101918:34) [<root>]\n at Object.onComplete (file:///data/data/org.nativescript.ngsample/files/app/vendor.js:101910:13) [<root>]"
} }
解决方案
您必须使用本地 IP,而不是localhost
因为您的模拟器/设备的 IP 会有所不同。
推荐阅读
- azure-active-directory - 是否可以将 $filters 与 msgraph /groups/delta 一起使用?
- javascript - 点击事件回调在用户第二次点击时被调用
- scala - 一个写得很糟糕的 Scala 代码。写得好怎么看?(玩弄编程范式和技术)
- python - Numpy数组没有重塑
- angular - TypeError:pna.nextTick 不是函数
- ruby-on-rails - 如何更改 Rails 5.2 url_for 超时
- c++ - Oracle 12.2 导致 Proc*C 应用程序崩溃
- python - 使用 linux 或 python 从文件中提取特定的列和字符串
- python - 如何调整 PlotWidget 的高度?
- python - 如何从集群实现 sklearn -AgglomerativeClustering?