复制代码 代码如下:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>AjaxHelloWorld</title>
<scripttype="text/javascript">
varxmlHttp;
functioncreateXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
}
elseif(window.XMLHttpRequest){
xmlHttp=newXMLHttpRequest();
}
}
functionstartRequest(){
createXMLHttpRequest();
try{
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET","data.xml",true);
xmlHttp.send(null);
}catch(exception){
alert("您要访问的资源不存在!");
}
}
functionhandleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200||xmlHttp.status==0){
//取得XML的DOM对象
varxmlDOM=xmlHttp.responseXML;
//取得XML文档的根
varroot=xmlDOM.documentElement;
try
{
//取得<info>结果
varinfo=root.getElementsByTagName('info');
//显示返回结果
alert("responseXML'svalue:"+info[0].firstChild.data);
}catch(exception)
{
}
}
}
}
</script>
</head>
<body>
<div>
<inputtype="button"value="returnajaxresponseXML'svalue"
onclick="startRequest();"/>
</div>
</body>
</html>
server.xml
复制代码 代码如下:
<?xmlversion="1.0"encoding="GB2312"?>
<root>
<info>helloworld!</info>
</root>