XMLHttpRequest: getResponseHeader() method - Web APIs | MDN
Syntax
js
getResponseHeader(headerName)
Parameters
headerName-
A string indicating the name of the header you want to return the text value of.
Return value
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.
Examples
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().
js
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 !== myExpectedType) {
client.abort();
}
}
};
Specifications
| Specification |
|---|
| XMLHttpRequest # dom-xmlhttprequest-getresponseheader |
Browser compatibility
See also
- Using XMLHttpRequest
- HTTP headers
getAllResponseHeaders()response- Setting request headers:
setRequestHeader()