Axios 需要手动配置响应中是否显示授权头部。可以使用如下代码示例解决:
import axios from 'axios';
axios.get('/something', {
headers: { Authorization: 'Bearer ' + token },
responseType: 'json',
withCredentials: true,
transformResponse: (response) => {
const parsedResponse = JSON.parse(response);
// Some code to handle the parsed response...
return parsedResponse;
},
})
.then((response) => {
// Handle the response...
})
.catch((error) => {
console.log(error.response.headers);
});
在上述代码中,withCredentials 属性设置为 true 以启用跨站点资源共享(CORS)请求。transformResponse 属性用于修改响应数据。可以在其中添加一些代码以打印授权头部信息。通过在控制台中查看输出结果,可以判断 Axios 是否成功将授权头部添加到响应中。