Android 如何使用nativescript代码连接到NodeJS服务器(无Express)。(“状态代码:未知错误”)

Android 如何使用nativescript代码连接到NodeJS服务器(无Express)。(“状态代码:未知错误”),android,nativescript,Android,Nativescript,我有一个nativescript共享代码连接到一个NodeJS API,它有另一个来源,我已经设置了CORS策略,它可以在web端工作,但是我在nativescript上得到了错误 任何帮助都将不胜感激 ERROR { "headers": { "normalizedNames": {}, "lazyUpdate": null, "headers": {} }, "status": 0, "statusText": "Unknown Error", "ur

我有一个nativescript共享代码连接到一个NodeJS API,它有另一个来源,我已经设置了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>]"
错误{
“标题”:{
“normalizedNames”:{},
“lazyUpdate”:空,
“标题”:{}
},
“状态”:0,
“statusText”:“未知错误”,
“url”:”http://localhost:4000/api/users",
“ok”:错,
“名称”:“HttpErrorResponse”,
“消息”:“的Http故障响应”http://localhost:4000/api/users: 0
未知错误“,
“错误”:{
“originalStack”:“错误:java.net.ConnectException:无法连接到新ZoneWareerRor上的localhost/127.0.0.1:4000\n(file:///data/data/org.nativescript.ngsample/files/app/vendor.js:82629:33)\n按请求完成(file:///data/data/org.nativescript.ngsample/files/app/vendor.js:101918:34)\n位于Object.onComplete(file:///data/data/org.nativescript.ngsample/files/app/vendor.js:101910:13)",
“zoneAwareStack”:“错误:java.net.ConnectException:无法在onRequestComplete连接到localhost/127.0.0.1:4000\n(file:///data/data/org.nativescript.ngsample/files/app/vendor.js:101918:34)[]\n位于Object.onComplete(file:///data/data/org.nativescript.ngsample/files/app/vendor.js:101910:13) []"
}
}

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

连接到localhost/127.0.0.1:4000似乎有问题,请检查IP,如果您不是从同一台机器访问,请使用机器IP而不是本地主机