You may need to get some content from the server at times, for example, you may want to "ping" the server and verify that the server is working correctly. At this point, you may just want to read the response header from the server and ignore the content. By reading the response header, you can derive the date content-type (content type), content-length (content length), and even last-modified (last modified).
If you are only focusing on the response header, the standard practice for completing such a request is to use the head request instead of the GET or POST request discussed earlier. When the server responds to the head request, it sends only the response header and ignores the content, even if the requested content can be returned to the browser, and the content is not actually returned. Because the content is ignored, the response to the head request is much smaller than the response to get or post.
Listing 4-3 shows a number of ways to get the response header from the XMLHttpRequest object, and describes how these methods are used in practice. This page has 4 links that correspond to each method of reading the response header from the XMLHttpRequest object.
Code Listings 4-3 readingresponseheaders.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<title>reading Response headers</title>
<script type= "Text/javascript" >
var xmlHttp;
var RequestType = "";
function Createxmlhttprequest () {
if (window. ActiveXObject) {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest ();
}
}
function doheadrequest (request, URL) {
RequestType = Request;
Createxmlhttprequest ();
Xmlhttp.onreadystatechange = Handlestatechange;
Xmlhttp.open ("Head", URL, True);
Xmlhttp.send (NULL);
}
function Handlestatechange () {
if (xmlhttp.readystate = = 4) {
if (RequestType = = "Allresponseheaders") {
getAllResponseHeaders ();
}
else if (RequestType = = "LastModified") {
Getlastmodified ();
}
else if (RequestType = = "Isresourceavailable") {
Getisresourceavailable ();
}
}
}
function getAllResponseHeaders () {
Alert (Xmlhttp.getallresponseheaders ());
}
function getlastmodified () {
Alert ("Last Modified:" + xmlhttp.getresponseheader ("last-modified"));
}
function getisresourceavailable () {
if (Xmlhttp.status = = 200) {
Alert ("successful response");
}
else if (Xmlhttp.status = 404) {
Alert ("Resource is unavailable");
}
else {
Alert ("Unexpected response status:" + Xmlhttp.status);
}
}
</script>
<body>
<a href= "javascript:doheadrequest (' allresponseheaders '),
' Readingresponseheaders.xml '); " >read all Response headers</a>
<br/>
<a href= "javascript:doheadrequest (' lastmodified '),
' Readingresponseheaders.xml '); " >get last Modified date</a>
<br/>
<a href= "javascript:doheadrequest (' isresourceavailable '),
' Readingresponseheaders.xml '); " >read Available resource</a>
<br/>
<a href= "javascript:doheadrequest (' isresourceavailable '),
' Not-available.xml '); " >read unavailable resource</a>
</body>