(vue.js)axios interceptors 拦截器中添加headers 属性

(vue.js)axios interceptors 拦截器中添加headers 属性:http://www.codes51.com/itwd/4282111.html

问题: (vue.js)axios interceptors 拦截器中添加headers 属性
描述:

已在网上查过怎么在 interceptors 中对header进行处理,
// http request 拦截器

axios.interceptors.request.use(

config => {
    if (store.state.token) {  // 判断是否存在token,如果存在的话,则每个http header都加上token
        config.headers.Authorization = `token ${store.state.token}`;
    }
    return config;
},
err => {
    return Promise.reject(err);

});

但是我要的是不是Authorization,而是自定义的 X-Auth-Token


也有看到在main.js中全局添加一个

axios.create({
  headers: 'X-Auth-Token'
});

查了好多都没有类似情况,在此提问求解。谢谢


解决方案1:

        return axios({
            method: 'get',
            url: url,
            headers: {'X-Auth-Token': 'your token'},
            withCredentials: true,
            params: paramsObj,
            responseType: 'json',
            timeout: 50000
        })

解决方案2:

这个是跨域的问题,需要后台处理进行允许跨域处理


以上介绍了“ (vue.js)axios interceptors 拦截器中添加headers 属性”的问题解答,希望对有需要的网友有所帮助。

原文地址:https://www.cnblogs.com/bydzhangxiaowei/p/10044899.html