Get the response Content-Type header from XHR

39,320

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>
Share:
39,320
700 Software
Author by

700 Software

Join 700.social ! A happy medium between Facebook and Gab. :) The name is too long but the domain looks good. Also, Software Development / Consulting (423) 802-8971 700software.com old username: George Bailey (but now I use my real name) http://www.google.com/images?q=George+Bailey

Updated on July 09, 2022

Comments

  • 700 Software
    700 Software almost 2 years

    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.

  • 700 Software
    700 Software over 13 years
    Why did you call it hand?
  • Quentin
    Quentin over 13 years
    Because I was too lazy to type out handler in full.
  • Ωmega
    Ωmega almost 11 years
    Would that work even with Transfer-Encoding: chunked when some headers are sent after the message body?
  • Dai
    Dai over 4 years
    @Ωmega I think it depends on the XHR's readyState (LOADING as opposed to DONE).
  • GChuf
    GChuf over 3 years
    Thanks, this will come in handy