XMLHttpRequest.setRequestHeader() 是设置HTTP请求头部的方法。此方法必须在 open()
方法和 send()
之间调用。如果多次对同一个请求头赋值,只会生成一个合并了多个值的请求头。
如果没有设置 Accept
属性,则此发送出send()
的值为此属性的默认值*/*
。
安全起见,有些请求头的值只能由user agent设置:forbidden header names和forbidden response header names.
自定义一些header属性进行跨域请求时,可能会遇到"not allowed by Access-Control-Allow-Headers in preflight response",你可能需要在你的服务端设置"Access-Control-Allow-Headers"。
语法
myReq.setRequestHeader(header, value);
参数
header
- 属性的名称。
value
- 属性的值。
说明
Specification | Status | Comment |
---|---|---|
XMLHttpRequest setRequestHeader() |
Living Standard | WHATWG living standard |
兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1 | ? | 5[1] 7 |
(Yes) | 1.2 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | 1.0 | (Yes) | ? | ? | ? |
[1] 使用ActiveXObject()实现的。Internet Explorer 7之后使用的是标准的XMLHttpRequest。