前端数据请求添加token验证
$.ajax({
type: "GET",
url: "/access/logout/" + userCode,
headers: {'Authorization': token}
});
$.ajax({
type: "GET",
url: "/access/logout/" + userCode,
beforeSend: function(request) {
request.setRequestHeader("Authorization", token);
},
success: function(result) {
}
});
import axios from 'axios';
const http = axios.create({
baseURL: 'https://api.example.com'
});
//Add a request interceptor -- 将token添加至请求头信息,发送至后台
http.interceptors.request.use((config)=>{
if(localStorage.getItem('token')){
config.headers.Authorization = localStorage.getItem('token') || '';
}
return config;
},(err)=>{
return Promise.reject(err);
});
// Add a response interceptor
http.interceptors.response.use(function (response) {
//针对响应结果做处理
return response;
}, function (error) {
return Promise.reject(error);
});
网友评论