在Axios请求中设置headers选项的Authorization属性,将授权信息作为值传递。示例如下:
import axios from 'axios';
// 设置授权信息
const token = 'xxxxxx';
const headers = {
'Authorization': `Bearer ${token}`
};
// 发起请求
axios.get('/api/data', {
headers: headers
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
注意,在设置Authorization时,需根据后台API的要求传递对应的授权方式和信息。同时还需排查其它可能引起问题的因素,比如请求URL地址、请求方法、网络问题等。