axios.interceptors.request.use(拦
2020-04-26 本文已影响0人
追风筝的Hassan
axios.interceptors.request.use(拦截器)
//请求
axios.interceptors.request.use(
function(config) {
if (config.method === 'get' && config.url != undefined) {
if (config.params == undefined) {
config.params = {}
}
config.params.__preventCache = new Date().getTime()
}
config.withCredentials = true // 允许携带token ,这个是解决跨域产生的相关问题
return config
},
function(error) {
return Promise.reject(error)
}
)
//返回状态判断
axios.interceptors.response.use(
response => {
return response
},
error => {
if (error.response.status == '401') {
if (process.env.NODE_ENV === 'local') {
window.location.href = '/#/login'
} else {
window.location.href =
loginUrl + '?app=7&redirect=' + encodeURIComponent(document.URL)
}
} else if (error.response.status == '400') {
ElementUI.Message.error(error.response.data.message)
} else {
ElementUI.Message.error(error.response.data.message)
}
return Promise.reject(error)
}
)