Headers() - Web APIs 编辑
The Headers()
constructor creates a new Headers
object.
Syntax
var myHeaders = new Headers(init);
Parameters
init
Optional- An object containing any HTTP headers that you want to pre-populate your
Headers
object with. This can be a simple object literal withByteString
values; or an existingHeaders
object. In the last case, the newHeaders
object inherits its data from the existingHeaders
object.
Example
Creating an empty Headers
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 Headers
object is created. In the following snippet we create a new Headers
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 Headers
object, passing it the first Headers
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
Specifications
Specification | Status | Comment |
---|---|---|
Fetch The definition of 'Headers()' in that specification. | Living Standard |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论