首页 > 解决方案 > 如何使用 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>]"

} }

标签: androidnativescript

解决方案


您必须使用本地 IP,而不是localhost因为您的模拟器/设备的 IP 会有所不同。


推荐阅读