This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available in Web Workers, except for Service Workers.
The XMLHttpRequest method getResponseHeader() returns the string containing the text of a particular header's value.
If there are multiple response headers with the same name, then their values are returned as a single concatenated string, where each value is separated from the previous one by a pair of comma and space. The getResponseHeader() method returns the value as a UTF byte sequence.
Note: The search for the header name is case-insensitive.
If you need to get the raw string of all of the headers, use the getAllResponseHeaders() method, which returns the entire raw header string.
getResponseHeader(headerName)
headerNameA string indicating the name of the header you want to return the text value of.
A string representing the header's text value, or null if either the response has not yet been received or the header doesn't exist in the response.
In this example, a request is created and sent, and a readystatechange handler is established to look for the readyState to indicate that the headers have been received; when that is the case, the value of the Content-Type header is fetched. If the Content-Type isn't the desired value, the XMLHttpRequest is canceled by calling abort().
const client = new XMLHttpRequest();
client.open("GET", "unicorns-are-awesome.txt", true);
client.send();
client.onreadystatechange = () => {
if (client.readyState === client.HEADERS_RECEIVED) {
const contentType = client.getResponseHeader("Content-Type");
if (contentType !== my_expected_type) {
client.abort();
}
}
};
| Specification |
|---|
| XMLHttpRequest> # dom-xmlhttprequest-getresponseheader> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
getResponseHeader |
1 | 12 | 1Starting from Firefox 49, empty headers are returned as empty strings in case the preferencenetwork.http.keep_empty_response_headers_as_empty_string is set to true, defaulting to false. Before Firefox 49 empty headers had been ignored. Since Firefox 50 the preference defaults to true. |
8 | 1.2 | 18 | 4Starting from Firefox for Android 49, empty headers are returned as empty strings in case the preferencenetwork.http.keep_empty_response_headers_as_empty_string is set to true, defaulting to false. Before Firefox for Android 49 empty headers had been ignored. Since Firefox for Android 50 the preference defaults to true. |
10.1 | 1 | 1.0 | 4.4 | 1 |
getAllResponseHeaders()responsesetRequestHeader()
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getResponseHeader