Request.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.
值
一个 Headers
对象。
示例
在下面的代码段中,我们使用 Request.Request()
构造函数(为获取与脚本处于同一目录的图片文件)创建新请求,然后将请求 headers 保存到变量中:
js
var myRequest = new Request("flowers.jpg");
var myHeaders = myRequest.headers; // Headers {}
使用 Headers.append
向 Headers
对象中添加 header;然后,使用第二个 init 参数创建一个新的 Request
,同时,传递 headers 作为一个 init 选项:
js
var myHeaders = new Headers();
myHeaders.append("Content-Type", "image/jpeg");
var myInit = {
method: "GET",
headers: myHeaders,
mode: "cors",
cache: "default",
};
var myRequest = new Request("flowers.jpg", myInit);
myContentType = myRequest.headers.get("Content-Type"); // returns 'image/jpeg'
规范
Specification |
---|
Fetch Standard # ref-for-dom-request-headers② |
浏览器兼容性
BCD tables only load in the browser