Headers() 构造函数创建新 对象。

句法

var myHeaders = new Headers(init);
					

参数

init 可选
An object containing any HTTP headers that you want to pre-populate your object with. This can be a simple object literal with ByteString values; or an existing object. In the last case, the new object inherits its data from the existing 对象。

范例

Creating an empty object is simple:

var myHeaders = new Headers(); // Currently empty
					

You could add a header to this using Headers.append :

myHeaders.append('Content-Type', 'image/jpeg');
myHeaders.get('Content-Type'); // Returns 'image/jpeg'
					

Or you can add the headers you want as the object is created. In the following snippet we create a new object, adding some headers by passing the constructor an init object as an argument:

var httpHeaders = { 'Content-Type' : 'image/jpeg', 'Accept-Charset' : 'utf-8', 'X-My-Custom-Header' : 'Zeke are cool' };
var myHeaders = new Headers(httpHeaders);
					

You can now create another object, passing it the first object as its init object:

var secondHeadersObj = new Headers(myHeaders);
secondHeadersObj.get('Content-Type'); // Would return 'image/jpeg' — it inherits it from the first headers object
					

规范

规范 状态 注释
Fetch
The definition of 'Headers()' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

No compatibility data found. Please contribute data for "api.Headers.headers" (depth: 1) to the MDN 兼容性数据存储库 .

另请参阅

元数据

  • 最后修改: