我的控制台看起来像这样
这是我的nginx cors设置
add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';
add_header 'Access-Control-Allow_Credentials' 'true';
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';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';
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';
add_header 'Access-Control-Allow_Credentials' 'true';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
proxy_redirect off;
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
我的节点服务器看起来像这样
var allowCrossDomain = function(req, res, next) {
if('GET' == req.method){
res.header('Access-Control-Allow-Origin', 'http://beloveddais.com');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
}
if('POST' == req.method){
res.header('Access-Control-Allow-Origin', 'http://beloveddais.com');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
}
if('OPTIONS' == req.method){
res.header('Access-Control-Allow-Origin', 'http://beloveddias.com');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.sendStatus(200);
}
next();
在我的浏览器控制台上,我收到了多个标题,只接受了一个。我该如何解决?我真的需要有人来帮助我。
答案 0 :(得分:0)
Access-Control-Allow-Origin
标题只应包含一个值作为允许的来源,或者您可以使用*来允许所有来源。
正如@TarunLalwani所评论的那样,您在NGINX配置和http://beloveddais.com
请求的代码中都将Access-Control-Allow-Origin
作为值添加到OPTIONS
标头中:
add_header 'Access-Control-Allow-Origin' 'http://beloveddais.com';
和
res.header('Access-Control-Allow-Origin', 'http://beloveddias.com');
根据您的设计选择删除其中一个,它应该没问题。