前后台分离,全局请求加头部
设置全局请求为ajax请求
_axios.interceptors.request.use( function(config) { var accessToken = localStorage.getItem(‘accessToken‘); if (accessToken && accessToken !== ‘‘) { config.headers.common[‘Authorization‘] = accessToken; } return config; }, function(error) { // Do something with request error return Promise.reject(error); } ); // 响应拦截器获取 headers,设置(刷新) Token // Add a response interceptor _axios.interceptors.response.use( function(response) { // Do something with response data var accessToken = response.headers[‘authorization‘]; if (accessToken && accessToken !== ‘‘) { localStorage.setItem(‘accessToken‘,accessToken); } return response; }, function(error) { // Do something with response error return Promise.reject(error); } );
原文地址:https://www.cnblogs.com/weibanggang/p/10416599.html
时间: 2024-10-14 13:09:06