Headers()
构造函数创建新
头
对象。
var myHeaders = new Headers(init);
init
可选
头
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. |
实时标准 |
No compatibility data found. Please contribute data for "api.Headers.headers" (depth: 1) to the MDN 兼容性数据存储库 .