Get the response Content-Type header from XHR

Bryan Field picture Bryan Field · Feb 5, 2011 · Viewed 37.1k times · Source

I would like to see whether the header was text/html or text/xml. If it was text/html then there was an error and I would rather catch that before proceeding.

Answer

Quentin picture Quentin · Feb 5, 2011

Use the getResponseHeader() method.

Minimal example:

<script>
function hand () {
        console.log(this.getResponseHeader('content-type'));
}
var x = new XMLHttpRequest();
x.onreadystatechange = hand;
x.open('GET', 'index.html', true);
x.send();
</script>