Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 761
700 Software Avatar asked Feb 05 '11 15:02

700 Software


People also ask

How do I get the response header value?

The getResponseHeader() method returns the value as a UTF byte sequence. Note: The search for the header name is case-insensitive. If you need to get the raw string of all of the headers, use the getAllResponseHeaders() method, which returns the entire raw header string.

How do I get headers in XMLHttpRequest?

setRequestHeader() The XMLHttpRequest method setRequestHeader() sets the value of an HTTP request header. When using setRequestHeader() , you must call it after calling open() , but before calling send() . If this method is called several times with the same header, the values are merged into one single request header.

Can Javascript read response header?

While you can't ready any headers of HTML response in JS, you can read Server-Timing header, and you can pass arbitrary key-value data through it.

How do I get response headers in node JS?

To get HTTP headers with Node. js, we can use the res. headers properties. const http = require("http"); const options = { method: "HEAD", host: "example.com", port: 80, path: "/", }; const req = http.


1 Answers

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> 
like image 117
Quentin Avatar answered Oct 09 '22 02:10

Quentin