XMLHttpRequest
方法
setRequestHeader()
sets the value of an HTTP request header. When using
setRequestHeader()
, you must call it after calling
open()
, but before calling
send()
. If this method is called several times with the same header, the values are merged into one single request header.
每次调用
setRequestHeader()
after the first time you call it, the specified text is appended to the end of the existing header's content.
若无
Accept
header has been set using this, an
Accept
header with the type
"*/*"
is sent with the request when
send()
被调用。
For security reasons, some headers can only be controlled by the user agent. These headers include the forbidden header names and forbidden response header names .
注意:
For your custom fields, you may encounter a "
not allowed by Access-Control-Allow-Headers in preflight response
" exception when you send requests across domains. In this situation, you need to set up the
Access-Control-Allow-Headers
in your response header at server side.
XMLHttpRequest.setRequestHeader(header, value)
header
The name of the header whose value is to be set.
value
The value to set as the body of the header.
undefined
.
| 规范 | 状态 | 注释 |
|---|---|---|
|
XMLHttpRequest
The definition of 'setRequestHeader()' in that specification. |
实时标准 | WHATWG (Web 超文本应用程序技术工作组) 实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
setRequestHeader
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5 | Opera 8 | Safari 1.2 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
完整支持
XMLHttpRequest