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.
get()
方法以 ByteString
形式从 Headers 对象中返回指定 header 的全部值。如果 Header 对象中不存在请求的 header,则返回 null
。
备注: 出于安全原因,部分头信息只能被用户代理控制。这些头信息包括 forbidden header names 和 forbidden response header names。
Syntax
js
myHeaders.get(name);
Parameters
Returns
以 ByteString
形式返回检索到的值。
Example
创建一个空的 Headers 对象:
js
var myHeaders = new Headers(); // Currently empty
可以通过 get() 方法来获取 header 中的值:
js
myHeaders.append("Content-Type", "image/jpeg");
myHeaders.get("Content-Type"); // Returns 'image/jpeg'
如果存在多个 header 值,那么只有第一个值会被返回:
js
myHeaders.append("Accept-Encoding", "deflate");
myHeaders.append("Accept-Encoding", "gzip");
myHeaders.get("Accept-Encoding"); // Returns "deflate,gzip"
备注: Headers.getAll
used to have this functionality, with Headers.get
returning only the first value added to the Headers
object. The latest spec has removed getAll()
, and updated get()
to return all values.
Specifications
Specification |
---|
Fetch Standard # ref-for-dom-headers-get① |
Browser compatibility
BCD tables only load in the browser