今天才写的AJAX JS类库,开源免费使用.非常简单方便.热乎乎的呢,献丑了...不好的大家一起改改啊!...

发个包上来 /Files/eicesoft/AJAX.zip完整的打包
  1  /* *
  2  AJAX类库 0.9
  3  @appellation    AJAX Lib 0.9
  4  @author         张怡冰
  5  @date           2007-08-01
  6  @copyright      ShanghaiLongzhong
  7  */
  8  function AJAXLib()
  9  {
 10       /* *
 11      成员变量
 12       */
 13       this .XMLHttpReq  =   null ;                         // XML对象
 14       this .method  =   " post " ;                         // 执行的方法(post/get)
 15       this .URLString  =   "" ;                         // 异步调用的页面地址
 16       this .response  =   "" ;                             // 异步返回的响应字符串
 17       this .responseXML  =   "" ;                       // 异步返回的响应XML
 18       this .failed  =   false ;                         // 创建对象错误标志
 19      
 20       /* *
 21      事件区
 22       */
 23       this .onLoading  =  function() { };             // 正在发送请求
 24       this .onLoaded  =  function() { };              // 已经接收到全部响应内容
 25       this .onInteractive  =  function() { };         // 正在解析响应内容
 26       this .onCompletion  =  function() { };          // 响应内容解析完成
 27       this .onError  =  function() { };               // 异步错误处理事件
 28       this .onFail  =  function() { };                // 创建对象失败处理世界
 29      
 30       /* *
 31      重置所有事件函数
 32       */
 33       this .resetFunctions  =  function() {
 34           this .onLoading  =  function() { };
 35           this .onLoaded  =  function() { };
 36           this .onInteractive  =  function() { };
 37           this .onCompletion  =  function() { };
 38           this .onError  =  function() { };
 39           this .onFail  =  function() { };
 40      };
 41      
 42       /* *
 43      初始化函数(构造时自动初始化)
 44       */
 45       this .Init  =  function()
 46      {
 47          // 对于Mozilla浏览器
 48           if (window.XMLHttpRequest)
 49          {
 50               // 直接使用XMLHttpRequest函数来创建XMLHttpRequest对象
 51               this .XMLHttpReq  =   new  XMLHttpRequest();
 52          }
 53           // 对于IE浏览器
 54           else   if  (window.ActiveXObject)
 55          {
 56               // 使用AcitveXObject函数创建浏览器
 57               try
 58              {
 59                   this .XMLHttpReq  =   new  ActiveXObject( " Msxml4.XMLHTTP " );
 60              }
 61               catch (e)
 62              {
 63                   try
 64                  {
 65                       this .XMLHttpReq  =   new  ActiveXObject( " Msxml3.XMLHTTP " );
 66                  }
 67                   catch (e)
 68                  {
 69                       try
 70                      {
 71                           this .XMLHttpReq  =   new  ActiveXObject( " Msxml2.XMLHTTP " );
 72                      }
 73                       catch (e)
 74                      {
 75                           try
 76                          {
 77                               this .XMLHttpReq  =   new  ActiveXObject( " Microsoft.XMLHTTP " );
 78                          } 
 79                           catch (oc)
 80                          {
 81                               this .failed = true ;    // 创建AJAX对象发生异常
 82                          }
 83                      }
 84                  }
 85              }
 86          }
 87      };
 88      
 89       /* *
 90      发送请求函数
 91      @param data 发送的数据
 92      @example send("id=1");
 93       */
 94       this .Send = function(data)
 95      {
 96          var self = this ;
 97           // 通过open方法取得与服务器的连接
 98           if ( this .method == " post " )
 99          {
100               this .XMLHttpReq.open(self.method,self.URLString, true );
101          }
102           else
103          {
104               this .XMLHttpReq.open(self.method,self.URLString + " ? " + encodeURI(data), true );
105          }
106           // 添加消息响应头
107           this .XMLHttpReq.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " );
108          
109           // 异步回调函数
110           this .XMLHttpReq.onreadystatechange  =  function()
111          {
112               // 对象未创建
113               if  (self.failed) {
114                  self.onFail();
115                   return ;
116              }
117              
118               // 消息响应标志
119               switch  (self.XMLHttpReq.readyState) {
120                   case   1 :
121                  {
122                      self.onLoading();
123                       break ;
124                  }
125                   case   2 :
126                  {
127                      self.onLoaded();
128                       break ;
129                  }
130                   case   3 :
131                  {
132                      self.onInteractive();
133                       break ;
134                  }
135                   case   4 :
136                  {
137                       if (self.XMLHttpReq.status == 200 ) { 
138                          self.response  =  self.XMLHttpReq.responseText;
139                          self.responseXML  =  self.XMLHttpReq.responseXML;
140                          self.onCompletion();
141                      }
142                       else  
143                      { 
144                          self.onError();      // 执行错误函数
145                      }
146                       break ;
147                  }
148              }
149          };
150           if ( this .method == " post " )
151          {
152               this .XMLHttpReq.send(encodeURI(data));  // 发送请求
153          }
154           else
155          {
156               this .XMLHttpReq.send();  // 发送请求
157          }
158      };
159      
160       this .Abort = function()
161      {
162           this .XMLHttpReq.abort();
163      }
164      
165       this .Close = function()
166      {
167           this .XMLHttpReq = null ;
168      }
169       // 初始化AJAX库
170       this .Init();
171  }

测试页代码(test.htm)
 1  < html >
 2  < head >
 3       < script language = " javascript "  src = " AJAXLib.js " ></ script >
 4       < script language = " javascript " > dot.gif
 5           var  st = new  AJAXLib();
 6          st.URLString = " a.aspx " ;
 7           // st.method="post"
 8          st.onCompletion = function ()
 9          dot.gif{
10              alert(st.response);
11          }
12          st.onLoading = function ()
13          dot.gif{
14              alert( " onLoading " );
15          }
16          st.onLoaded = function ()
17          dot.gif{
18              alert( " onLoaded " );
19          }
20          st.onInteractive = function ()
21          dot.gif{
22              alert( " onInteractive " );
23          }
24          st.Send( " id=1&name=张怡冰 " );
25       </ script >
26  </ head >
27  < body >
28      
29  </ body >
30  </ html >

服务器端代码(a.asp)

1  < %
2       Dim  id1,name1
3      id1 = Request.QueryString( " id " )
4      name1 = Request.QueryString( " name " )
5      Response.Write( " 你好: " + name1 + " 你的ID是 " + id1)
6  % >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值