(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 屬性”的問題解答,希望對有需要的網友有所幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM