在一个Headers对象内部,Headers接口的append()方法可以追加一个新值到已存在的headers上,或者新增一个原本不存在的header。

Headers.set 和 append() 两者之间的不同之处在于当指定header是已经存在的并且允许接收多个值时,Headers.set会重写此值为新值,而append()会追加到值序列的尾部。

因为安全性原因,一些headers仅受用户代理控制。包括forbidden header namesforbidden response header names

语法

myHeaders.append(name,value);

参数

name
要追加给Headers对象的HTTP header名称.
value
要追加给Headers对象的HTTP header值.

返回

Void.

例程

创建一个空的Headers对象:

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

可以通过append()方法添加header:

myHeaders.append('Content-Type', 'image/jpeg');
myHeaders.get('Content-Type'); // Returns 'image/jpeg'

如果指定header不存在, append()将会添加这个header并赋值 . 如果指定header已存在并允许有多个值, append()将会把指定值添加到值队列的末尾。

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

要使用新值覆盖旧值,请使用Headers.set

规范

Specification Status Comment
Fetch
append()
Living Standard  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
append
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 14Firefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Full support 10.1WebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile Full support YesFirefox Android No support NoOpera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS No support NoSamsung Internet Android Full support 4.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.

 

参见

文档标签和贡献者

标签: 
此页面的贡献者: jason-grimm, mdnwebdocs-bot, Taoja
最后编辑者: jason-grimm,