axios - 在 vue3 响应中获取 Bearer null
问题描述
我正在使用 axios 发布数据,并且我想要来自响应标头的授权令牌。
await axios.post('authenticate', {
params: {
one: this.one,
two: this.two,
},
}).then((res) => { console.log(res.config.headers.authorization); }); # returns Bearer null
当我查看“网络”选项卡并且发布请求在响应标头中具有承载值并且在请求标头中具有“Bearer null”时,我收到了选项和发布响应。我显然只是从中获取请求标头res
,我无法弄清楚如何获取响应标头授权值。res.headers
根本没有 Authorization 属性。只有这个:
{
"cache-control": "no-cache, no-store, max-age=0, must-revalidate",
"content-length": "0",
"expires": "0",
"pragma": "no-cache"
}
如何访问响应标头的授权值?
解决方案
响应标头存储在headers
(不是config.headers
,这是请求配置的标头)。
假设响应实际上包含Authorization
标头,它可以通过以下方式访问res.headers.Authorization
:
axios.post('authenticate', {
params: { /*...*/ },
}).then((res) => {
console.log(res.headers.Authorization)
})
但是正如您所指出的,res.headers
它不包含该标头,因此后端没有填充它。
推荐阅读
- nginx - 在 vitejs 中构建和使用基本属性时出现“加载模块脚本失败”错误
- c - 如何测试 _Static_assert 是否已定义?
- html - Outlook 365 的 HMTL 代码呈现问题
- android - Android - 使用公共 crt 和私有 pem 文件连接到后端服务器
- javascript - 如何修复 CSS 中段落文本的文本溢出?
- c# - 如何在字符串中包含数字而不转换为另一种语言(例如阿拉伯语)
- vb.net - 将面板从winform中取出并使其可移动
- html - vuetify 中的垂直表头
- jmeter - CSV 文件中的 7000 多条记录,用于 JMeter 测试计划中的 100 个线程
- python - 使用 Here API 服务计算斜率的问题