怎么把token加入到http头部啊?

挑战
  • 604
怎么把token加入到http头部啊,在这里可以实现吗?
  $axios.onRequest((config) => {
    if (!config.headers.common) {
      config.headers.common = {};
    }
    config.headers.common.token = config.headers.common.Authorization || '';

    return config;
  });

图片描述

回复
阅读 4.1k
3 个回答
service.interceptors.request.use(config => {
  // Do something before request is sent
  if (store.getters.token) {
     config.headers['键'] = store.getters.token; 
  }
  return config;
}, error => {
  // Do something with request error
  //console.log(error); // for debug
  Promise.reject(error);
})
derek
  • 92

this.axios.interceptors.request.use(function(config) {

config.headers['token'] = yourToken  
return config;

},function(err){

   return Promise.reject(error);

});

headers: {'X-Requested-With': 'XMLHttpRequest'},
宣传栏