700 Software
700 Software

Reputation: 87903

Get the response Content-Type header from XHR

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.

Upvotes: 44

Views: 42649

Answers (1)

Quentin
Quentin

Reputation: 944568

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>

Upvotes: 66

Related Questions