【问题标题】:How to get headers from http response in VueJS?如何从 VueJS 中的 http 响应中获取标头?
【发布时间】:2020-09-04 21:46:59
【问题描述】:

我使用 axios 从后端检索一些数据。我发送一个请求,我有一个响应。我想阅读我的一些标题。我已经尝试了很多事情,但没有人在工作。 我的标头包含以下标头'content-type', content-length', 'x-wp-total', 'x-wp-totalpages'

我尝试使用response.headers('content-type') 获取标头,但我收到错误response.headers is not function。我有麻烦了。

我的问题似乎很简单。从响应中获取标头的语法是什么?

【问题讨论】:

    标签: vue.js axios http-headers response response-headers


    【解决方案1】:

    你看过https://github.com/axios/axios#response-schema吗?

      // `headers` the HTTP headers that the server responded with
      // All header names are lower cased and can be accessed using the bracket notation.
      // Example: `response.headers['content-type']`
      headers: {},
    

    相关:Response header fields and CORS

    【讨论】:

      【解决方案2】:

      根据您的日志,它应该是response.headers["content-type"]。或者您也可以拨打response.headers.contentType 通常也可以。

      【讨论】:

        【解决方案3】:

        headers 是响应的属性。它是一个对象而不是函数,因此您不能将其用作“headers()”之类的函数。如果你想访问headers的属性,你应该使用'.'或 []

        【讨论】:

          猜你喜欢
          • 2017-11-06
          • 2016-09-21
          • 2015-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-28
          相关资源
          最近更新 更多