axios 攔截器+token


參考:https://www.cnblogs.com/feijiediyimei/p/9940937.html
因為使用了jwt鑒權,需要每次請求的時候帶上token。
在main.js中加入axios的全局攔截器可解。

// 添加一個請求攔截器
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  //這里經常搭配token使用,將token值配置到tokenkey中,將tokenkey放在請求頭中
  config.headers['Authorization'] = token;
  
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// 添加一個響應攔截器
axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });


免責聲明!

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



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