使用Ajax技术从本地取回xml文件信息

来源:互联网 发布:企业公告软件哪家好 编辑:程序博客网 时间:2024/06/05 09:03

1. 编写note.xml

  

<note><to>Mr.Dylan</to><body>Don't forget the meeting!</body></note>

2.编写getxml.html

<html><head><script type="text/javascript">var xmlhttp;function loadXMLDoc(url){xmlhttp=null;if (window.XMLHttpRequest)  {// code for IE7, Firefox, Opera, etc.  xmlhttp=new XMLHttpRequest();  }else if (window.ActiveXObject)  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }if (xmlhttp!=null)  {  xmlhttp.onreadystatechange=state_Change;  xmlhttp.open("GET",url,true);  xmlhttp.send(null);  }else  {  alert("Your browser does not support XMLHTTP.");  }}function state_Change(){if (xmlhttp.readyState==4)  {// 4 = "loaded"  if (xmlhttp.status==200 || xmlhttp.status==0)    {// 200 = "OK"    document.getElementById('A1').innerHTML=xmlhttp.status;    document.getElementById('A2').innerHTML=xmlhttp.statusText;    document.getElementById('A3').innerHTML=xmlhttp.responseText;    }  if(xmlhttp.status==404)    {      alert("Page not found!");    }  else     {    alert("Problem retrieving XML data:" + xmlhttp.statusText);    }  }}</script></head><body><h2>Using the HttpRequest Object</h2><p><b>Status:</b><span id="A1"></span></p><p><b>Status text:</b><span id="A2"></span></p><p><b>Response:</b><br /><span id="A3"></span></p><button onclick="loadXMLDoc('note.xml')">Get XML</button></body></html>

注:红色部分,由于获取的是本地的XML所以此处浏览器返回的xmlhttp.status=0.

也就是说没有通过Web服务器形式的Ajax请求返回值都是0;

3. 使用火狐打开getxml.html:


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

present by dylan.


原创粉丝点击