Headers

Headers interface of the 抓取 API allows you to perform various actions on HTTP request and response headers . These actions include retrieving, setting, adding to, and removing headers from the list of the request's headers. A Headers object has an associated header list, which is initially empty and consists of zero or more name and value pairs. You can add to this using methods like append() (见 范例 .) In all methods of this interface, header names are matched by case-insensitive byte sequence.

For security reasons, some headers can only be controlled by the user agent. These headers include the forbidden header names and forbidden response header names .

A Headers object also has an associated guard, which takes a value of immutable , request , request-no-cors , response ,或 none . This affects whether the set() , delete() ,和 append() methods will mutate the header. For more information see Guard .

You can retrieve a Headers object via the Request.headers and Response.headers properties, and create a new Headers object using the Headers.Headers() 构造函数。

An object implementing Headers can directly be used in a for...of structure, instead of entries() : for (var p of myHeaders) 相当于 for (var p of myHeaders.entries()) .

注意 : you can find more out about the available headers by reading our HTTP headers reference.

构造函数

Headers()
创建新的 Headers 对象。

方法

Headers.append()
Appends a new value onto an existing header inside a Headers object, or adds the header if it does not already exist.
Headers.delete()
Deletes a header from a Headers 对象。
Headers.entries()
Returns an iterator allowing to go through all key/value pairs contained in this object.
Headers.forEach()
Executes a provided function once for each array element.
Headers.get()
返回 ByteString sequence of all the values of a header within a Headers object with a given name.
Headers.has()
Returns a boolean stating whether a Headers object contains a certain header.
Headers.keys()
Returns an iterator allowing you to go through all keys of the key/value pairs contained in this object.
Headers.set()
Sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist.
Headers.values()
Returns an iterator allowing you to go through all values of the key/value pairs contained in this object.

注意 : To be clear, the difference between Headers.set() and Headers.append() is that if the specified header does already exist and does accept multiple values, Headers.set() will overwrite the existing value with the new one, whereas Headers.append() will append the new value onto the end of the set of values. See their dedicated pages for example code.

注意 : All of the Headers methods will throw a TypeError if you try to pass in a reference to a name that isn't a valid HTTP Header name . The mutation operations will throw a TypeError if the header has an immutable Guard . In any other failure case they fail silently.

注意 : When Header values are iterated over, they are automatically sorted in lexicographical order, and values from duplicate header names are combined.

Obsolete methods

Headers.getAll()
Used to return an array of all the values of a header within a Headers object with a given name; this method has now been deleted from the spec, and Headers.get() now returns all values of a given name instead of just the first one.

范例

In the following snippet, we create a new header using the Headers() constructor, add a new header to it using append() , then return that header value using get() :

var myHeaders = new Headers();
myHeaders.append('Content-Type', 'text/xml');
myHeaders.get('Content-Type') // should return 'text/xml'
					

The same can be achieved by passing an array of arrays or an object literal to the constructor:

var myHeaders = new Headers({
    'Content-Type': 'text/xml'
});
// or, using an array of arrays:
myHeaders = new Headers([
    ['Content-Type', 'text/xml']
]);
myHeaders.get('Content-Type') // should return 'text/xml'
					

规范

规范 状态 Comment
Fetch
The definition of 'Headers' in that specification.
实时标准

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Headers Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ≤18 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42 Chrome Android 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android 44 Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android 4.0
Headers() 构造函数
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ≤79
≤79
≤79
Disabled
Disabled From version ≤79: this feature is behind the Experimental Web Platform Features preference.
Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42 Chrome Android 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the preference.
Safari iOS No Samsung Internet Android 4.0
append
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42 Chrome Android 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android 4.0
delete
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42 Chrome Android 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android 4.0
entries
Chrome 45 Edge 16 Firefox 44 IE No Opera 32 Safari ? WebView Android 45 Chrome Android 45 Firefox Android 44 Opera Android 32 Safari iOS No Samsung Internet Android 5.0
get
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 52
52
Prior to Firefox 52, get() only returned the first value in the specified header, with getAll() returning all values. From 52 onwards, get() now returns all values and getAll() has been deleted.
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari 10.1 WebView Android 42 Chrome Android 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android 4.0
getAll
弃用 非标
Chrome 42 — 60 Edge ≤18 — 79 Firefox 39 — 52
不支持 39 — 52
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29 — 47 Safari No WebView Android 42 — 60 Chrome Android 42 — 60 Firefox Android No Opera Android 29 — 44 Safari iOS No Samsung Internet Android 4.0
has
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android 42 Chrome Android 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android 4.0
keys
Chrome 45 Edge 16 Firefox 44 IE No Opera 32 Safari ? WebView Android 45 Chrome Android 45 Firefox Android 44 Opera Android 32 Safari iOS No Samsung Internet Android 5.0
Lexicographical sorting, and values from duplicate header names combined when iterated. Chrome ? Edge ? Firefox 44 IE No Opera 28 Safari No WebView Android ? Chrome Android ? Firefox Android No Opera Android ? Safari iOS No Samsung Internet Android ?
set
Chrome 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge 14 Firefox 39
39
34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No Opera 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No WebView Android 42 Chrome Android 42
42
41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android No Opera Android 29
29
28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No Samsung Internet Android 4.0
values
Chrome 45 Edge 16 Firefox 44 IE No Opera 32 Safari ? WebView Android 45 Chrome Android 45 Firefox Android 44 Opera Android 32 Safari iOS No Samsung Internet Android 5.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅