My First Ajax
最後更新:2018-12-07
來源:互聯網
上載者:User
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>My First Ajax</TITLE>
</HEAD><BODY>
<script>
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
xmlHttp=new window.ActiveXObject("Microsoft.XMLHttp");
else if(window.XMLHttpRequest)
xmlHttp=new window.XMLHttpRequest();
} function startRequest()
{
createXMLHttpRequest();
xmlHttp.OnReadyStateChange=HandleStateChange;
xmlHttp.open("Get","data.xml",true);
xmlHttp.send(null);
} function HandleStateChange()
{
if(xmlHttp.ReadyState==4)
{
if(xmlHttp.Status==200)
{
try
{
simple();
}
catch (exception)
{
alert("error");
}
}
} } function simple()
{
var str="";
var pro=document.ajax.name.value;
var xmlDoc=xmlHttp.responseXML;
var JiangNode=xmlDoc.getElementsByTagName("pro")[0];
//alert(JiangNode.getAttribute("name"));
//alert(pro);
var detailNode=JiangNode.getElementsByTagName("detail"); for(i=0;i<detailNode.length;i++)
{
if(str=="")
{
str=str+detailNode[i].childNodes[0].nodeValue;
}
else
{
str=str+","+detailNode[i].childNodes[0].nodeValue;
}
}
document.ajax.name.value=str;
}
</script>
<form action=# name="ajax">
<input type=text name="name"></input>
<input type=button value="Get" onclick="startRequest()"></input>
</form>
</BODY>
</HTML> <?xml version="1.0" encoding="big5" ?>
<root>
<pro name="江蘇省">
<detail>南京</detail>
<detail>蘇州</detail>
</pro>
<pro name="浙江省">
<detail>杭州</detail>
<detail>寧波</detail>
</pro>
</root>