nginx配置跨域如下
location /minio/ {
# 跨域配置
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,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Pragma,Content-Type,Authorization,Origin';
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,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Pragma,Content-Type,Authorization,Origin';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
# 自定义转发
proxy_pass http://192.168.0.1:9800/;
}
注意: 如果使用了自定义的请求头,如token等,必须在nginx配置放行,否则仍然会报跨域错误
检查跨域的js代码,f12之后复制到console执行
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1/name');
xhr.send(null);
xhr.onload = function(e) {
var xhr = e.target;
console.log(xhr.responseText);
}