欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

axios全局請(qǐng)求參數(shù)設(shè)置,請(qǐng)求及返回?cái)r截器的方法

 更新時(shí)間:2018年03月05日 10:26:31   作者:panyox  
下面小編就為大家分享一篇axios全局請(qǐng)求參數(shù)設(shè)置,請(qǐng)求及返回?cái)r截器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

應(yīng)用場(chǎng)景:

1,每個(gè)請(qǐng)求都帶上的參數(shù),比如token,時(shí)間戳等。

2,對(duì)返回的狀態(tài)進(jìn)行判斷,比如token是否過(guò)期

代碼如下:

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 已過(guò)期,重定向到登錄頁(yè)面
	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全局請(qǐng)求參數(shù)設(shè)置,請(qǐng)求及返回?cái)r截器的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論