从 JSON 响应访问 [Symbol(Response internals)]

Eig*_*ght 4 javascript json header fetch

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

    const res = await fetch(
      `url`
    );
Run Code Online (Sandbox Code Playgroud)

要访问身体,我只需要做

    const response = await res.json()
Run Code Online (Sandbox Code Playgroud)

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

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
  }
}
Run Code Online (Sandbox Code Playgroud)

什么Symbol(Response internals)?我如何访问它的headers属性?

小智 6

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

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

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

  • 它不会按照问题中的要求获取“[Symbol(响应内部)]”。 (2认同)