Headers:get() 方法
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.
备注: 此特性在 Web Worker 中可用。
Headers
接口的 get()
方法返回一个字节字符串,其中包含一个具有给定名称的 Headers
对象中的标头的所有值。如果请求的标头不存在于 Headers
对象中,则返回 null
。
语法
js
get(name)
参数
返回值
一个 String
序列,代表检索到的标头的值,如果未设置该标头,则为 null
。
示例
创建一个空的 Headers
对象非常简单:
js
const myHeaders = new Headers(); // 当前为空
myHeaders.get("Not-Set"); // 返回 null
可以通过 get()
方法来获取 header 中的值:
js
myHeaders.append("Content-Type", "image/jpeg");
myHeaders.get("Content-Type"); // 返回“image/jpeg”
如果标头有多个相关值,字节字符串将按照添加到标头对象的顺序包含所有值:
js
myHeaders.append("Accept-Encoding", "deflate");
myHeaders.append("Accept-Encoding", "gzip");
myHeaders.get("Accept-Encoding"); // 返回“deflate, gzip”
myHeaders
.get("Accept-Encoding")
.split(",")
.map((v) => v.trimStart()); // 返回 [ "deflate", "gzip" ]
规范
Specification |
---|
Fetch # ref-for-dom-headers-get① |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
get |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.
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.