记一次后端拿不到自定义请求头的经历

1.公司项目用户账号密码登录,使用了rsa加密,针对不同版本做了不同需求,不影响之前的版本,

使用了自定义请求头“yx_version”

//拦截器设置全局请求token
Vue.http.interceptors.push((request, next) => {
        if(localStorage.getItem('userInfo')!="null"&&localStorage.getItem('userInfo')!=""&&localStorage.getItem('userInfo')!=null){
            request.headers.set('Authorization',JSON.parse(localStorage.getItem('userInfo')).token); //设置请求头
            // request.headers.set('Content-Type','application/json;charset=UTF-8'); //设置请求头
        }
        
        request.headers.set('us','web'); //设置web后台标识
        request.headers.set('yx_version','2.10'); //rsa加密版本
        //设置请求时间
        let timeout;
        // 如果某个请求设置了_timeout,那么超过该时间,会终端该(abort)请求,并执行请求设置的钩子函数onTimeout方法,不会执行then方法。
        if (request._timeout) {
            timeout = setTimeout(() => {
                if(request.onTimeout) {
                    request.onTimeout(request);
                    request.abort()
                }  
            }, request._timeout);
        }
        next((response) => {
            clearTimeout(timeout);
            return response
        })
})

2.后台本地测试是可以拿到,部署到服务器后拿不到

3.本地没问题,为什么发布之后就有问题,不是跨域问题,那就是环境问题,第一个要检查的就是nginx,因为本地没有nginx,服务器上有

解决方法

nginx里面有个underscores_in_headers 配置,默认是off,这个配置是默认忽略掉请求header里
面的"_"的,改成on即可,或者把请求头里别用"_"

 

posted @ 2020-09-03 10:27  奔跑吧前端(李钊)  阅读(987)  评论(0编辑  收藏  举报