Access to XMLHttpRequest at ‘***’ from origin ‘**’ has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header contains multiple values ', *’, but only one is allowed.
错误如下:
出现了跨域问题,网上关于跨域问题的解决方法很多,我说说关于我自己是怎么解决的。
我用了nginx,nginx配置如下:
server {
#nginx的默认端口
listen 9001;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~/educms/ {
proxy_pass http://localhost:8004;
}
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
本意是想在访问educms时转发到对应端口8004,结果出现跨域。
后端接口使用了@CrossOrgin注解:
经过网上查询发现可能是在nginx和controller中同时使用了跨域处理。
注解掉nginx中三行代码即可。
nginx配置:
server {
#nginx的默认端口
listen 9001;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~/educms/ {
proxy_pass http://localhost:8004;
}
#add_header Access-Control-Allow-Origin *;
#add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
#add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';