Basic Ajax tutorial (5) _ read Response Header

Source: Internet
Author: User

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<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>

</Head>

<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>

</Html>

 

ReadingResponseHeaders. xml file

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<ReadingResponseHeaders>

</ReadingResponseHeaders>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.