Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<Html>
<Head>
<Title> Ajax Hello World </title>
<Script type = "text/javascript">
Var xmlHttp;
Function createXMLHttpRequest (){
If (window. ActiveXObject ){
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
}
}
Function startRequest (){
CreateXMLHttpRequest ();
Try {
XmlHttp. onreadystatechange = handleStateChange;
XmlHttp. open ("GET", "data. xml", true );
XmlHttp. send (null );
} Catch (exception ){
Alert ("The resource you want to access does not exist! ");
}
}
Function handleStateChange (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 | xmlHttp. status = 0 ){
// Retrieve the xml dom object
Var xmlDOM = xmlHttp. responseXML;
// Get the root of the XML document
Var root = xmlDOM.doc umentElement;
Try
{
// Obtain the <info> result
Var info = root. getElementsByTagName ('info ');
// Display the returned results
Alert ("responseXML's value:" + info [0]. firstChild. data );
} Catch (exception)
{
}
}
}
}
</Script>
</Head>
<Body>
<Div>
<Input type = "button" value = "return ajax responseXML's value"
Onclick = "startRequest ();"/>
</Div>
</Body>
</Html>
Server. xml
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "GB2312"?>
<Root>
<Info> hello world! </Info>
</Root>