ajax请求egg用nginx转发跨域问题

火狐浏览器报的

谷歌浏览器报的

前提:

npm i egg-cors --save

config 文件下的pulgin.js 已经添加

//启用跨域支持
exports.cors = {
    enable: true,
    package: 'egg-cors',
}

和 config.defaule.js 已经添加

//加密设置
    config.security = {
        csrf: false,
        ctoken: false,
        domainWhiteList: ['localhost:7002'] //允许跨域的白名单,为false时不限制跨域
    }

还出现上面的情况的话按照下面的操作

解决办法配置nginx 的请求获取数据的接口 添加如下代码

location ^~ /apiInfo {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

        proxy_pass  http://yaoshistatic;

}

  

原文地址:https://www.cnblogs.com/yu-hailong/p/10893700.html