从JSON响应访问[Symbol(Response内部)) [英] Accessing [Symbol(Response internals)] from JSON response

查看:117
本文介绍了从JSON响应访问[Symbol(Response内部))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the library isomorphic-unfetch (https://www.npmjs.com/package/isomorphic-unfetch) to get JSON data from a Rest API. This is how I make the request:

    const res = await fetch(
      `url`
    );

要进入身体,我只需要做

To access the body I simply need to do

    const response = await res.json()

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

But how do I access the response headers? If I log the response to my server's console this is what I see:

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(响应内部)?以及如何访问其 headers 属性?

What's Symbol(Response internals)? And how do I access its headers property?

推荐答案

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

To access its headers use one of the following:

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

这篇关于从JSON响应访问[Symbol(Response内部))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆