Angular 6-http错误状态码0,而不是401、400 ... 200以外的任何状态

时间:2018-10-29 06:59:30

标签: angular nginx cors angular6

我知道许多热心人士已经在这里提供了类似的解决方案, Link-1 Link-2

但是我已经配置了CORS。因此我正在使用NGiNX服务器并查看以下配置,以满足CORS的问题...

if ($request_method = 'OPTIONS') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Credentials' 'true';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,x-auth-token,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      add_header 'Access-Control-Expose-Headers' 'Location,DNT,x-auth-token,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      add_header 'Access-Control-Expose-Headers' 'Origin, X-Requested-With, X-AUTH-TOKEN, Content-Type, Accept, Authorization, Location, Cache-Control, User-Agent, Keep-Alive';
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain charset=UTF-8';
      add_header 'Content-Length' 0;
      return 204;
}
if ($request_method = 'POST') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Credentials' 'true';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,x-auth-token,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      add_header 'Access-Control-Expose-Headers' 'DNT,x-auth-token,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      add_header 'Access-Control-Expose-Headers' 'Origin, X-Requested-With, X-AUTH-TOKEN, Content-Type, Accept, Authorization, Location, Cache-Control, User-Agent, Keep-Alive';
}
if ($request_method = 'GET') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Credentials' 'true';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,x-auth-token,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      add_header 'Access-Control-Expose-Headers' 'DNT,x-auth-token,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      add_header 'Access-Control-Expose-Headers' 'Origin, X-Requested-With, X-AUTH-TOKEN, Content-Type, Accept, Authorization, Location, Cache-Control, User-Agent, Keep-Alive';
} 

请参见以下屏幕截图,该屏幕截图是我从浏览器(Chrome)的控制台中捕获的,以便向所有人突出显示。 enter image description here

为什么Angular无法正确捕获状态代码401(或除200外的任何状态代码)。每次都给出“ 0”而不是有效的数字。

注意-早在几年前它就可以正常工作。因此我认为CORS设置已正确处理。这就是为什么我不确定不确定怎么回事。

任何帮助都非常可观!!

更新- 根据此link,他们建议添加“ WWW-Authenticate”标题。需要吗?因此,这只是导致我当前的问题?

0 个答案:

没有答案