目录
1、请求拦截器
1.1 在请求出发之前设置一些信息
格式:
aixos.interceptors.request.use(function(config){
//在请求出发之前进行一些设置
return config;
},function(err){
//处理响应的错误信息
console.log(err)
})
实例:
aixos.interceptors.request.use(function(config){
//在请求出发之前进行一些设置
config.headers.mytoken='test'
return config;
},function(err){
//处理响应的错误信息
console.log(err)
})
axios.get('http://host/port/path').then(function(data){
console.log(data)
})
2、响应拦截器
2.1 在获取数据之前对数据做一些加工处理
格式:
aixos.interceptors.response.use(function(res){
//在这里对返回的数据进行处理
return res;
},function(err){
//处理响应的错误信息
console.log(err)
})
实例:
aixos.interceptors.response.use(function(res){
//这里res是对象
return res.data;
},function(err){
//处理响应的错误信息
console.log(err)
})
axios.get('http://host/port/path').then(function(data){
//这里的data数据是经过上面的响应拦截器处理过的
console.log(data)
})