美文网首页
Vue使用proxy提示 “Refused to set uns

Vue使用proxy提示 “Refused to set uns

作者: 动感光波波波 | 来源:发表于2019-10-30 15:41 被阅读0次

    在调用另一个源节点接口的时候,报错提示
    “Refused to set unsafe header "Referer"


    报错信息

    经过查询发现是因为浏览器拒绝人为设置伪装的referer
    正巧不巧,在用vue的proxy来进行跨域处理,由此记录一下用法。

    1. 这是在调用axios的方法
    method:'post',
        url:"/api/v1/home/page",
        headers:{
            'Content-Type':'application/x-www-form-urlencoded',
            'Referer':'https://xxxxx.com'
        }
    

    显然这样子浏览器处于安全考虑是不允许发送出去的

    1. 这里是vue-config.js文件的配置
    module.exports = {
        publicPath: './',  
        lintOnSave: true,
        devServer:{
            proxy:{
                "/api":{                        //设置跨域变量代号
                    target:"xxxx.com/",         //你想要代理的目标源链接
                    changeOrigin:true,          //开启代理
                    pathRewrite:{               //设置二级
                        "^/api": "/",           
                        "^/api2": "/body"
                    },
                    headers:{//重点在这里,添加配置项 headers 就可以了
                        'Referer':'https://xxxx.com'
                    }
                }
            }
        }
    }
    

    3.最后成功地拿到了数据


    完成

    相关文章

      网友评论

          本文标题:Vue使用proxy提示 “Refused to set uns

          本文链接:https://www.haomeiwen.com/subject/lrgovctx.html