下面我就为大家分享一篇axios全局请求参数设置,请求及返回拦截器的方法,具有很好的参考价值,希望对大家有所帮助。
应用场景:
1,每个请求都带上的参数,比如token,时间戳等。
2,对返回的状态进行判断,比如token是否过期
代码如下:
axios.interceptors.request.use(
config => {
var xtoken = getXtoken()
if(xtoken != null){
config.headers['X-Token'] = xtoken
}
if(config.method=='post'){
config.data = {
...config.data,
_t: Date.parse(new Date())/1000,
}
}else if(config.method=='get'){
config.params = {
_t: Date.parse(new Date())/1000,
...config.params
}
}
return config
},function(error){
return Promise.reject(error)
}
)
axios.interceptors.response.use(function (response) {
// token 已过期,重定向到登录页面
if (response.data.code == 4){
localStorage.clear()
router.replace({
path: '/signin',
query: {redirect: router.currentRoute.fullPath}
})
}
return response
}, function (error) {
// Do something with response error
return Promise.reject(error)
})上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
以上就是通过axios全局请求参数设置请求以及返回拦截器操作步骤有哪些?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号