token设置请求头
1.直接获取本地存储中的token数据
instance.interceptors.request.use((config)=>{
const token = sessionStorage.getItem('ji')
if (token) {
config.headers.authorization = token
}
return config
},error=>{
return Promise.reject(error)
})
2.自定义headers
var instance =axios.create({
url:'',
baseURL:'',
method:"get",
timeout:4000,
headers: {'X-Auth-Token': 'your token'},
withCredentials: true,
params:'',
responseType: 'json',
})
instance.interceptors.request.use((config)=>{
let TOKEN='数据'
if(TOKEN){
config.headers['X-Auth-Token']=`${TOKEN}`;
}
return config
},error=>{
return Promise.reject(error)
})
转载请注明原文地址:https://blackberry.8miu.com/read-33903.html