qs = qs.js 库
axios
axios.interceptors.request.use(config => {
let storage=window.localStorage;
/*拦截请求添加token*/
if(storage){
let info = JSON.parse(storage['ngStorage-userInfo']);
let data = qs.parse(config.data);
data.token = info.token;
config.data = qs.stringify(data);
}
/** loading-box{ **/
// let loading = Loading.service({
// fullscreen: true,
// text: '拼命加载中...'
// /** loading-box} **/
// })
return config
}
ajax
/*ajax 拦截添加token到body*/
$.ajaxSetup({
dataType: "json",
cache: false,
beforeSend: function() {
let data = qs.parse(arguments[1].data);
data.token = store.state.info.token;
arguments[1].data = qs.stringify(data);
}
});
/*ajax拦截添加token到Header*/
let token = Cookies.get("Token");
$.ajaxSetup({
dataType: "json",
cache: false,
beforeSend: function(request) {
request.setRequestHeader("token", token);
},
dataFilter: function(data) {
let newData = $.parseJSON(data).message
if(newData && newData.data && newData.data.State && newData.data.State == 401){
return Promise.reject('token验证错误!')
}
return data
}
});
网友评论