The XMLHttpRequest.setRequestHeader() method sets the value of an HTTP request header. You must call setRequestHeader()
after open()
, but before send()
. If this method is called several times with the same header, the values are merged into one single request header.
If no Accept
header has been set using this, an Accept
header with the */*
is sent with the request when send()
is called.
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 "not allowed by Access-Control-Allow-Headers in preflight response" exception when you send request to cross domain. In this situation, you need set "Access-Control-Allow-Headers" in your response header at server side.
XMLHttpRequest.setRequestHeader(header, value)
header
value
Specification | Status | Comment |
---|---|---|
XMLHttpRequest The definition of 'setRequestHeader()' in that specification. | Living Standard | WHATWG living standard |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 1 | (Yes) | ? | 5[1] 7 | (Yes) | 1.2 |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | 1.0 | (Yes) | (Yes) | ? | ? | ? |
[1] This feature was implemented via ActiveXObject(). Internet Explorer implements the standard XMLHttpRequest since version 7.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader