内容简介:今天碰到一个跨域请求的问题。不做任何处理,正常情况下回报以下错误:
今天碰到一个跨域请求的问题。 a.domain.com 域名下需要请求 b.domain.com 下的接口。
不做任何处理,正常情况下回报以下错误:
解决方案
浏览器端需要对发起的请求做跨域处理。设置 xhr 的属性 withCredentials = true 。
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/', true);
xhr.withCredentials = true;
xhr.send(null);
Nginx也需要做对应的 Access-Control-Allow-Origin 设置。
if ($http_origin ~* ^https://[a-z\\d]+\.domain\.com$){
add_header 'Access-Control-Allow-Origin' '$http_origin';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Cookie,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
只要是 *.domain.com 的域名都可以正常访问。
这样,就可以满足了。
以上所述就是小编给大家介绍的《Nginx跨域请求配置》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
XML、JSON 在线转换
在线XML、JSON转换工具
RGB CMYK 转换工具
RGB CMYK 互转工具