Ajax Basics Tutorial (4)-Implementing basic AJAX Technology 4.2 Read response header

Source: Internet
Author: User
Tags resource

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.