Ajax库的编写及使用

ajax使用在服务器端。

ajax.js

 1 function ajax(url,fnSucc,fnFail)
 2 {
 3     //1.创建ajax对象
 4     var oAjax = null;
 5     if(window.XMLHttpRequest)
 6     {
 7         oAjax = new XMLHttpRequest();
 8     }else
 9     {
10         oAjax = new ActiveXObject("Microsoft.XMLHttp");
11     }
12 
13     //2.连接服务器
14     oAjax.open('GET',url,true);
15 
16     //3.发送请求
17     oAjax.send();
18 
19     //4.接受服务器的返回
20     oAjax.onreadystatechange = function()
21     {
22         if(oAjax.raedyState==4)
23         {
24             if(oAjax.status==200)
25             {
26                 fnSucc(oAjax.responseText);
27             }else
28             {
29                 if(fnFail)
30                 {
31                     fnFail(oAjax.status);
32                 }
33             }
34         }
35     };
36 };

html:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <script src="ajax.js"></script>
 7     <script>
 8 
 9         //读取一个本地文件 adc.txt
10         ajax('abc.txt',function(str){alert(str)},function(){alert('读取失败')});
11     </script>
12 </head>
13 <body>
14     
15 </body>
16 </html>

 

posted @ 2016-03-31 11:06  蔷薇蔓蔓  阅读(193)  评论(0编辑  收藏  举报