我有一个angular 6应用程序和一个API网关。我呼叫的网址之一是https://api.mydomain.com/api/person/1。
当我进入网站时,可以在Chrome的“网络”标签中看到api端点被调用。但是,呼叫失败。我将api网址复制到浏览器中,可以看到json消息。我也尝试过邮递员,在那里我可以看到生成的json消息。因此,端点可以工作并生成正确的JSON消息。
当我重新加载网站并检查浏览器控制台时,我会看到各种CORS消息。这是我的Nginx设置:
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Length' 0;
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Access-Control-Allow-Headers' Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
proxy_redirect off;
proxy_set_header host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
}
Chrome似乎确实对CORS标头做了些奇怪的事情。有人知道如何解决这个问题吗?