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

来源:互联网 发布:java绘制动态仪表盘 编辑:程序博客网 时间:2024/05/22 14:14
  1. //1. 编写note.xml
  2. <note>  
  3. <to>Mr.Dylan</to>  
  4. <body>Don't forget the meeting!</body>  
  5. </note>  

    2.编写getxml.html

    [html] view plain copy
    1. <html>  
    2. <head>  
    3. <script type="text/javascript">  
    4. var xmlhttp;  
    5. function loadXMLDoc(url)  
    6. {  
    7. xmlhttp=null;  
    8. if (window.XMLHttpRequest)  
    9.   {// code for IE7, Firefox, Opera, etc.  
    10.   xmlhttp=new XMLHttpRequest();  
    11.   }  
    12. else if (window.ActiveXObject)  
    13.   {// code for IE6, IE5  
    14.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  
    15.   }  
    16. if (xmlhttp!=null)  
    17.   {  
    18.   xmlhttp.onreadystatechange=state_Change;  
    19.   xmlhttp.open("GET",url,true);  
    20.   xmlhttp.send(null);  
    21.   }  
    22. else  
    23.   {  
    24.   alert("Your browser does not support XMLHTTP.");  
    25.   }  
    26. }  
    27.   
    28. function state_Change()  
    29. {  
    30. if (xmlhttp.readyState==4)  
    31.   {// 4 = "loaded"  
    32.   <span style="color:#FF0000;">if (xmlhttp.status==200 || xmlhttp.status==0)</span>  
    33.     {// 200 = "OK"  
    34.     document.getElementById('A1').innerHTML=xmlhttp.status;  
    35.     document.getElementById('A2').innerHTML=xmlhttp.statusText;  
    36.     document.getElementById('A3').innerHTML=xmlhttp.responseText;  
    37.     }  
    38.   if(xmlhttp.status==404)  
    39.     {  
    40.       alert("Page not found!");  
    41.     }  
    42.   else   
    43.     {  
    44.     alert("Problem retrieving XML data:" + xmlhttp.statusText);  
    45.     }  
    46.   }  
    47. }  
    48. </script>  
    49. </head>  
    50.   
    51. <body>  
    52. <h2>Using the HttpRequest Object</h2>  
    53.   
    54. <p><b>Status:</b>  
    55. <span id="A1"></span>  
    56. </p>  
    57.   
    58. <p><b>Status text:</b>  
    59. <span id="A2"></span>  
    60. </p>  
    61.   
    62. <p><b>Response:</b>  
    63. <br /><span id="A3"></span>  
    64. </p>  
    65.   
    66. <button onclick="loadXMLDoc('note.xml')">Get XML</button>  
    67.   
    68. </body>  
    69. </html>  

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

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

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


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

    present by dylan.


0 0
原创粉丝点击