前段跨域问题 check: No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

跨域报错:cess to XMLHttpRequest at 'http://mycelium.test/api/PluginController/getVoiceConfig' from origin 'http://localhost:63343' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

看到这哦吼跨域了 第一想法 nginx 添加跨域头和php入口文件加上跨域头(业务需要只允许/api/PluginController的支持跨域)

location /api/PluginController {
  add_header 'Access-Control-Allow-Origin' '*';

  xxxxx............
}

 

报错依旧。感觉不对都给了咋还报错呢。查下nginx瞅瞅

 请求进来了access.log  状态都是500 一查长知识了   跨域请求浏览器会发送两次请求,预请求通过了才是真正的数据请求

 最后找到解决方案直接预请求返回 2xx

 

location /api/PluginController {
  add_header 'Access-Control-Allow-Origin' '*';
  if ($request_method = 'OPTIONS') {
        return 200;
    }
  xxxxx............
}

 到这错误又变了

cess to XMLHttpRequest at 'http://mycelium.test/api/PluginController/getVoiceConfig' from origin 'http://localhost:63343' has been blocked by CORS policy: Request header field x-mc-checksum is not allowed by Access-Control-Allow-Headers in preflight response.

因为业务需要自己定义了几个特殊的Header字段导致不允许,加个配置通过下

location /api/PluginController {
  add_header 'Access-Control-Allow-Origin' '*';
if ($request_method = 'OPTIONS') {
        return 200;
    }
add_header 'Access-Control-Allow-Headers' 'x-mc-from,x-mc-checksum,x-mc-product,access-control-allow-origin,User-Agent,Cache-Control,Content-Type,Range';

  xxxxx............
}

到此问题处理完毕

最后给个建议:安全起见不建议Access-Control-Allow-Origin直接为 ‘*’ 最好是指定域名或二级域名

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值