Headers.getAll()

此页面上有脚本错误。虽然这条信息是写给网站编辑的,但您也可以在下面查看部分内容。

这篇翻译不完整。请帮忙从英语翻译这篇文章

{{APIRef("Fetch")}}{{ SeeCompatTable() }}

getAll() 方法会以数组形式返回指定header的所有值. 如果指定的header未存在,则返回一个空数组.

Note:出于安全原因, 部分头信息只能被用户代理控制. 这些头信息包括 {{Glossary("Forbidden_header_name", "forbidden header names", 1)}}  和 {{Glossary("Forbidden_response_header_name", "forbidden response header names", 1)}}.

Syntax

myHeaders.getAll(name);

Parameters

name
需检索的HTTP header名称. 如果HTTP header中不存在指定名称则抛出一个 {{jsxref("TypeError")}}.

Returns

An {{domxref("Array")}} containing a {{domxref("ByteString")}} sequence representing the values of the retrieved header.

Example

Creating an empty Headers object is simple:

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

You could add a header to this using {{domxref("Headers.append")}}, then retrieve it using getAll():

myHeaders.append('Content-Type', 'image/jpeg');
myHeaders.getAll('Content-Type'); // Returns [ "image/jpeg" ]

If the header has multiple values associated with it, the array will contain all the values, in the order they were added to the Headers object:

myHeaders.append('Accept-Encoding', 'deflate');
myHeaders.append('Accept-Encoding', 'gzip');
myHeaders.getAll('Accept-Encoding'); // Returns [ "deflate", "gzip" ]

Note: Use {{domxref("Headers.get")}} to return only the first value added to the Headers object.

Specifications

Specification Status Comment
{{SpecName('Fetch','#dom-headers-getall','getAll()')}} {{Spec2('Fetch')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{ CompatChrome(42) }}
{{ CompatChrome(41) }} behind pref
 
{{ CompatGeckoDesktop(39)}}
34 behind pref
{{ CompatNo }}

29
28 behind pref

{{ CompatNo }}
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}

See also

文档标签和贡献者

此页面的贡献者: Taoja
最后编辑者: Taoja,