您好,欢迎来到豪特汽车网。
搜索
您的当前位置:首页axios全局请求参数设置,请求及返回的方法

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全局请求参数设置,请求及返回的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

Copyright © 2019- howto234.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务