【问题标题】:Getting Bearer null in vue3 response在 vue3 响应中获取 Bearer null
【发布时间】:2021-07-11 15:28:50
【问题描述】:

我正在使用 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"
}

如何访问响应标头的授权值?

【问题讨论】:

    标签: axios request response vuejs3


    【解决方案1】:

    响应标头存储在headers(不是config.headers,它们是来自请求配置的标头)。

    假设响应实际上包含Authorization 标头,则可以通过res.headers.Authorization 访问它:

    axios.post('authenticate', {
      params: { /*...*/ },
    }).then((res) => {
      console.log(res.headers.Authorization)
    })
    

    但正如您所指出的,res.headers 不包含该标头,因此后端没有填充它。

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 1970-01-01
      • 2022-12-31
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多