Ajax读取Xml精简示例

来源:互联网 发布:淘宝购物车图标logo 编辑:程序博客网 时间:2024/05/24 03:22

 

示例共两个文件:AjaxXml.html和Request.xml复制代码放在同一目录下点击按钮即可看到效果

AjaxXml.html

-----------------------------------------------------------------------------------------------------------------------------------

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title> </title> 
<script language="javascript">

var xmlhttp;

function createXMLHttpRequest() 

if(window.ActiveXObject) 

xmlhttp 
= new ActiveXObject("Microsoft.XMLHTTP"); 

else if(window.XMLHttpRequest) 

xmlhttp 
= new XMLHttpRequest(); 

}

function startRequest() 

createXMLHttpRequest(); 
xmlhttp.onreadystatechange 
= handleStateChange; 
xmlhttp.open(
"GET","Request.xml",false); 
xmlhttp.send(
null);

}

function handleStateChange() 

if(xmlhttp.readyState == 4
{
document.getElementById(
"results").innerHTML = xmlhttp.responseText;  


}

</script> 
</head>

<body>

<form action="#">

<input type="button" value="xmlRead" onclick="startRequest();" />

</form>

<div id="results"> </div>

</body> 
</html>


*******************************************************************************************************************

Request.xml文件

-----------------------------------------------------------------------------------------------------------------------------------

<table border="1"> 
<tbody> 
<tr> 
<th>time </th> 
<th>Country </th> 
<th>City</th> 
<th>Games</th> 
</tr> 
<tr> 
<th>2008 </th> 
<th>China </th> 
<th>BeiJing </th> 
<th>Olympics </th> 
</tr> 
</tbody> 
</table>

-----------------------------------------------------------------------------------------------------------------------------------
 
原创粉丝点击