为什么错误处理时 AngularJS 错误消息为空?

Why is AngularJS error message empty on error handling?

我正在使用 AngularJS 代码向服务器发出请求。成功时它工作正常,但是当我将请求重定向到其他故意引发 CORS 问题的域时,将调用错误处理函数但 errorData 为空。在 Chrome 中,当我调试时,我可以看到错误函数被命中,并且我还在控制台中收到消息 "No 'Access-Control-Allow-Origin' header is present on the requested resource." 但我无法从代码访问它。如何记录此错误?

RemoteService.doRequestPromise().then(
function(resp) {
    LEService.logIt(resp);
    if (status != 200) {
        LEService.error(resp);
    }
},
function(errorData) {
    LEService.error({ 'err': errorData });
}).finally(function() {
    usSpinnerService.stop('spin1');
});

这是 angular 为您提供的 failed CORS request:

{
  "data": null,
  "status": 0,
  "config": {
    "method": "GET",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "url": "https://some.site/thing",
    "headers": {
      "Accept": "application/json, text/plain, */*"
    }
  },
  "statusText": ""
}

与 CORS 无关,但您的状态为 0 且为空 statusText。在其他情况下,您收到状态 0 的情况可能不多,但如果您绝对需要确保这是一个 CORS 错误,则值得进行更多研究。