Response:headers 属性
headers 属性是响应头,一个 Headers 对象。
值
- 只读。给它赋值抛
TypeError。 - 响应头的名字不区分大小写:
get('content-type')和get('CONTENT-TYPE')得到同一个值。 - 服务器返回了多个同名的响应头时,
get()得到用,连接起来的一个字符串。多条Set-Cookie用getSetCookie()分别读。 - 自己用
new Response()创建时,是init.headers,没写Content-Type时按内容补一个默认的。
示例
js
async function main() {
const res = await fetch('https://httpbin.org/get');
console.log(res.headers.get('content-type'));
console.log(res.headers.get('CONTENT-TYPE'));
}
main();结果:两行都输出 application/json。
和 Web 的差别
和 Web 相同。
参见
Headers:读、遍历响应头的方法