【问题标题】:Accessing [Symbol(Response internals)] from JSON response从 JSON 响应访问 [Symbol(Response internals)]
【发布时间】:2020-02-25 11:43:32
【问题描述】:

我正在使用库 isomorphic-unfetch (https://www.npmjs.com/package/isomorphic-unfetch) 从 Rest API 获取 JSON 数据。这就是我提出请求的方式:

    const res = await fetch(
      `url`
    );

要访问正文,我只需要这样做

    const response = await res.json()

但是如何访问响应标头?如果我将响应记录到我的服务器控制台,这就是我所看到的:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    // stuff
  },
  [Symbol(Response internals)]: {
    url: 'request url',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

Symbol(Response internals) 是什么?以及如何访问其headers 属性?

【问题讨论】:

    标签: javascript json header fetch-api


    【解决方案1】:

    要访问其headers,请使用以下方法之一:

    const res = await fetch(url);
    console.log(res.headers.get('content-type');
    // or
    res.headers.forEach(header => console.log(header));
    

    https://github.github.io/fetch/#Headers

    【讨论】:

    • 非常感谢。我一直在远程“调试”我的应用程序以使用 console.log 在请求对象上找到适当的字段,但是在记录时缺少 headers 属性。知道为什么会这样吗?
    猜你喜欢
    • 2021-07-04
    • 2023-04-08
    • 2017-04-19
    • 2018-04-10
    • 1970-01-01
    • 2010-11-19
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多