The value of the ‘Access-Control-Allow-Origin‘ header in the response must not be the wildcard ‘*‘ w

The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’.

No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

problem

nginx添加cors header

add_header Access-Control-Allow-Origin '*';
# 报错:
# The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's  credentials mode is 'include'.

如果有多个location,每个里面都配置一遍 Access-Control-Allow-Origin 则会报错

# 报错:
# No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

reason

  1. 配置域名需要具体
  2. 不要在每个location里面配置一遍cors

solution

# 完整配置
server {
    listen       7006;
    # 1. 不能匹配为 '*' 需要具体到某个地址
    # 2. 配置不能写在location里面
    add_header Access-Control-Allow-Origin http://localhost:8000;
    add_header Access-Control-Allow-Credentials true;

    # http://localhost:7006/test/a.js
    # dist目录下的子文件也可以访问到 
    location /test1 {
        alias /Users/project/dist1/;
    }

    location /test2 {
        alias /Users/project/dist2/;
    }    
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值