1 <!DOCTYPE html>
2 <html>
3 <head lang="en">
4 <meta charset="UTF-8">
5 <title></title>
6 <script>
7 window.onload = function () {
8 var oBtn = document.getElementById("btn1");
9
10 oBtn.onclick = function () {
11 // 1. 创建Ajax对象
12 var oAjax = null;
13 if (window.XMLHttpRequest) {
14 oAjax = new XMLHttpRequest();
15 } else {
16 oAjax = new ActiveXObject("Microsof.XMLHTTP");
17 }
18
19 // 2. 连接服务器
20 oAjax.open("GET", "aaa.txt?t="+new Date().getTime(), true);
21
22 // 3. 发送请求
23 oAjax.send();
24
25 // 4. 接收返回信息
26 oAjax.onreadystatechange = function () {
27 // oAjax.readyState 浏览器和服务器的交互进行到哪一步了
28 if (oAjax.readyState == 4) { // 读取完成
29 if (oAjax.status == 200) {
30 alert(oAjax.responseText);
31 } else {
32 alert("读取失败");
33 }
34 }
35 }
36 }
37 }
38 </script>
39 </head>
40 <body>
41 <input id="btn1" type="button" value="读取" />
42 </body>
43 </html>