Loading

Ajax_使用XMLHttpRequest实现

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8" />
 5     <title>Document</title>
 6     <script type="text/javascript">
 7     window.onload = function () {
 8         // 1.获取a节点,并为其添加 onclick 相应函数
 9         document.getElementsByTagName("a")[0].onclick = function(){
10             //3.创建一个XMLHttpRequest 对象
11             var request = new XMLHttpRequest();
12 
13             //4.准备发送请求的数据:url
14             var url = this.href + "?time=" + new Date();
15             var method = "GET";
16 
17             //5.调用XMLHttpRequest 对象的 open 方法
18             request.open(method, url);
19 
20             //6.XMLHttpRequest 对象的 send 方法
21             request.send(null);
22 
23             //7.为XMLHttpRequest 对象添加 onreadystatechange 相应函数,由服务器触发
24             request.onreadystatechange = function () {
25 
26                 //8.判断响应是否完成:XMLHttpRequest 对象的 readyState 属性值为 4 的时候
27                 if (request.readyState == 4) {
28                     //9.再判断响应是否可用: XMLHttpRequest 对象 status 属性值为 200
29                     if (request.status == 200 || request.status ==304) {
30                     //10.打印响应结果:responseText
31                     alert(request.responseText);
32                     }
33                 }
34             }
35 
36             //2.取消a的默认行为
37             return false;
38         }
39     }
40     </script>
41 </head>
42 <body>
43     <a href="hellpAjax.txt">hellpAjax</a>
44 </body>
45 </html>

 

posted @ 2015-09-28 14:59  狂扫一条街  阅读(151)  评论(0编辑  收藏  举报