axios.defaults.headers.post['Content-type'] = 'application/x-www-form-urlencoded'

axios.interceptors.request.use(
  request => {
    let token = JSON.parse(localStorage.getItem("token"));
    if (token) {
      request.headers['Token'] = token;
    }
    return request;
  },
  function (error) {
    return Promise.reject(error);
  }
);

axios.interceptors.response.use((r)=>{
	let token = JSON.parse(localStorage.getItem('token'));
	if(r.headers.token){
		const setToken = JSON.stringify(r.headers.token)
		localStorage.setItem("token",setToken)
	}
	if(r.data.code == 503 ){
		localStorage.clear();
		location.href = '/Login'
	}
	return r;
	},function(error){
		return Promise.reject(error)
	}
)
Logo

前往低代码交流专区

更多推荐