浪子Javascript - Ajax函数

 1 //****************************************************************************************
 2 //* Description : 浪子Javascript - Ajax函数(ALoafer Javascript).
 3 //* Author   : Cabo'liu    caboliu@163.com
 4 //* Version   : 1.0.0
 5 //* History     : 2009.03.25  Created
 6 //****************************************************************************************
 7 
 8 //****************************************************************************************
 9 //file:aloaferajax.js
10 //****************************************************************************************
11 function Ajax(){
12   this.setXMLHttpRequest = function(){
13     var http_request = null;
14     if(window.XMLHttpRequest){
15       http_request = new XMLHttpRequest();
16       if(http_request.overrideMimeType) http_request.overrideMimeType("text/xml");
17     }
18      else if(window.ActiveXObject){
19        try{
20        http_request = new ActiveXObject("Msxml2.XMLHTTP");
21        }
22        catch(e){
23          try{
24          http_request = new ActiveXObject("Microsoft.XMLHTTP");
25        }
26          catch(e){
27            alert(e);
28            //Brower antique.
29          }
30        }
31      }
32      else{
33        alert(e);
34        //Brower undefined .
35      }
36      return http_request;
37   };
38   this.handleStateChange = function(http_request,callback){
39     if(http_request.readyState == 4){
40       if(http_request.status == 200 || http_request.status == 0){
41         if(typeof(callback) == "function"){
42           callback(http_request.responseText);
43         }
44         else{
45           alert(http_request.responseText);
46         }
47       }
48     }
49   };
50   this.send = function(type, url, sync,sendStr,callback){
51     var oThis = this;
52     var http_request = this.setXMLHttpRequest();
53     if(http_request != null){
54       http_request.onreadystatechange = function() { oThis.handleStateChange(http_request,callback); };
55       http_request.open(type, url, sync);
56       http_request.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
57       http_request.send(sendStr);
58    }
59   };
60   this.post = function(url,sendStr,callback) { this.send("POST", url, true, sendStr,callback); };
61   this.get = function(url,callback) { this.send("GET", url, truenull, callback); };
62 }

 1 //****************************************************************************************
 2 //file: index.html
 3 //****************************************************************************************
 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 5 <html>
 6 <head>
 7 <title>浪子Javascript - AJAX函数测试</title>
 8 <script type="text/javascript" src="loaferajax.js"></script>
 9 <script type="text/javascript">
10 function setAjax(){
11   var ajax = new Ajax();
12   var url = "data.txt";
13   ajax.get(
14     url,
15     function(msg){
16       alert("输出结果: " + msg);  //输出结果: 浪子Javascript-AJAX返回值!
17      }
18   );
19 }
20 </script>
21 </head>
22 <body>
23   <input id="btn" name="btn" type="button" value="AJAX 提交测试" onclick="javascript:setAjax();" />
24 </body>
25 </html>

 

1 //****************************************************************************************
2 //file: data.txt
3 //****************************************************************************************
4 浪子Javascript-AJAX返回值!



posted @ 2010-08-13 11:25  偶滴怪怪  阅读(345)  评论(0)    收藏  举报