Basic Auth认证

axios配置如下只需要增加 auth 参数即可,auth: {username: 'lanpz',password: '123456789'}

1
2
3
4
5
axios.post('xxx', {x: 1}, auth: {username: 'lanpz',password: '123456789'} , {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}).then(({data}) => console.log(data));

Bearer Token

在axios请求拦截器里面自定义下请求头即可。

1
2
3
service.interceptors.request.use(async request => {
request.headers['Authorization'] = `Bearer ${token}`
})