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 超文本应用程序技术工作组) 实时标准

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: