Response.headers

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

Response 接口的只读属性 headers 包含与响应关联的Headers对象。

语法

var myHeaders = response.headers;

一个 Headers 对象。

例程

在我们的 Fetch Response example 例程中(详见 Fetch Response live),我们使用Request()构造函数创建了一个新的Request对象,传入了一个 jpg 路径。我们接着使用fetch()触发了请求,用Body.blob从响应中提取了 blob 实例,使用URL.createObjectURL创建了一个 URL 对象,然后显示在了<img>中。

注意,在fetch()的顶级块中我们输出了headers到控制台。

js
var myImage = document.querySelector("img");

var myRequest = new Request("flowers.jpg");

fetch(myRequest).then(function (response) {
  console.log(response.headers); // returns a Headers{} object
  response.blob().then(function (myBlob) {
    var objectURL = URL.createObjectURL(myBlob);
    myImage.src = objectURL;
  });
});

规范

Specification
Fetch
# ref-for-dom-response-headers①

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
headers

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also