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

Accessing [Symbol(Response internals)] from JSON response(从 JSON 响应访问 [Symbol(Response internals)])
本文介绍了从 JSON 响应访问 [Symbol(Response internals)]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

什么是符号(响应内部)?以及如何访问它的 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 internals)]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

jasmine.createSpyObj with properties(jasmine.createSpyObj 与属性)
What#39;s a good way to reuse test code using Jasmine?(使用 Jasmine 重用测试代码的好方法是什么?)
Does Jasmine#39;s toThrow matcher require the argument to be wrapped in an anonymous function?(Jasmine 的 toThrow 匹配器是否需要将参数包装在匿名函数中?)
Jasmine - Spying on a method call within a constructor(Jasmine - 在构造函数中监视方法调用)
Getting requirejs to work with Jasmine(让 requirejs 与 Jasmine 一起工作)
How can I spy on a getter property using jasmine?(如何使用 jasmine 监视 getter 属性?)