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
| 归档时间: |
|
| 查看次数: |
4192 次 |
| 最近记录: |