Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Retrieving HTTP Headers

var headers = XMLHttp.getAllResponseHeaders();

The method getAllResponseHeaders() (see preceding code) returns all HTTP headers in the HTTP response, whereas the getResponseHeader() method returns just one specific header. The following code shows how to get information about the type of web server used:

Retrieving an HTTP Response Header (responseheader.html)

<script language="JavaScript"
  type="text/javascript" src="xmlhttp.js"></script>
<script language="JavaScript"
  type="text/javascript">
var XMLHttp = getXMLHttp();
XMLHttp.open("GET", "phrasebook.txt");
XMLHttp.onreadystatechange = handlerFunction;
XMLHttp.send(null);

function handlerFunction() {
  if (XMLHttp.readyState == 4) {
    var servertype =
      XMLHttp.getResponseHeader("Server");
    window.alert("Web server used: " + servertype);
  }
}
</script>

Note that not all servers send this header; some also put fake data in it to make server profiling harder.

  • + Share This
  • 🔖 Save To Your Account