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>
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, 2022Comments
-
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 over 13 yearsWhy did you call it
hand
? -
Quentin over 13 yearsBecause I was too lazy to type out
handler
in full. -
Ωmega almost 11 yearsWould that work even with
Transfer-Encoding: chunked
when some headers are sent after the message body? -
Dai over 4 years@Ωmega I think it depends on the XHR's readyState (
LOADING
as opposed toDONE
). -
GChuf over 3 yearsThanks, this will come in handy