IPv6域名去访问IPv4域名底下的js文件报错
贴一个报错代码
Access to XMLHttpRequest at 'http://wlsp.icu:81/usr/plugins/Pio/models/pio/model.moc' from origin 'http://bt.wlsp.icu' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
l2d.js:1 GET http://wlsp.icu:81/usr/plugins/Pio/models/pio/model.moc net::ERR_FAILED
解决问题
- 分析是由于浏览器发现请求不同域名拒绝服务造成的
需要构造请求头来证明资源可以使用(服务器为nginx)
server { listen 8999; listen 80; listen [::]:80; listen 9999; listen [::]:9999; listen [::]:8999; server_name 192.168.0.109 wlsp.icu bt.wlsp.icu; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/typecho; # 在nginx服务器中添加下列代码 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';} # 解释 Access-Control-Allow-Origin 服务器默认是不被允许跨域的。给Nginx服务器配置`Access-Control-Allow-Origin *`后,表示服务器可以接受所有的请求源(Origin),即接受所有跨域的请求。 Access-Control-Allow-Headers 是为了防止出现以下错误: Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response. 这个错误表示当前请求Content-Type的值不被支持。其实是我们发起了"application/json"的类型请求导致的。这里涉及到一个概念:预检请求(preflight request),请看下面"预检请求"的介绍。 Access-Control-Allow-Methods 是为了防止出现以下错误: Content-Type is not allowed by Access-Control-Allow-Headers in preflight response. 给OPTIONS 添加 204的返回,是为了处理在发送POST请求时Nginx依然拒绝访问的错误 发送"预检请求"时,需要用到方法 OPTIONS ,所以服务器需要允许该方法。
- 贴一下大佬博客
评论 (2)