問題描述
我正在使用庫 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)]的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!