XMLHttpRequest实例

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 5 <title>Document</title>
 6 <script type="text/javascript">
 7 var xmlhttp;
 8 function loadXMLDoc(url){
 9     xmlhttp = null;
10     if(window.XMLHttpRequest){
11         xmlhttp = new XMLHttpRequest();
12     }else if(window.ActiveXObject){
13         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
14     }
15     if(xmlhttp!=null){
16         xmlhttp.onreadystatechange = state_Change;
17         xmlhttp.open("GET", url, true);
18         xmlhttp.send(null);
19     }else{
20         alert("Your brower does not support XMLHTTP");
21     }
22 }
23 function state_Change(){
24     if(xmlhttp.readyState == 4){
25         if(xmlhttp.status == 200){
26             document.getElementById('A1').innerHTML = xmlhttp.status;
27             document.getElementById('A2').innerHTML = xmlhttp.statusText;
28             document.getElementById('A3').innerHTML = xmlhttp.responseText;
29         }else{
30             alert("Problem retrieving XML data:" + xmlhttp.statusText);
31         }
32     }
33 }
34 </script>
35 </head>
36 <body>
37     <h2>Using the HttpRequest</h2>
38     <p>
39         <b>Status:</b>
40         <span id = "A1"></span>
41     </p>
42     <p>
43         <b>status text:</b>
44         <span id = "A2"></span>
45     </p>
46     <p>
47         <b>Response:</b>
48         <br /><span id = "A3"></span>
49     </p>
50 
51     <button onclick="loadXMLDoc('note.xml')">Get XML</button>
52 </body>
53 </html>
<?xml version="1.0" encoding = "ISO-8859-1"?>
<note>
    <to>George</to>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
</note>
原文地址:https://www.cnblogs.com/hell0x/p/5261065.html