用JS类实现简单Ajax效果

来源:互联网 发布:苏联集体农庄 知乎 编辑:程序博客网 时间:2024/05/17 02:07

Ajax.js:

  1. /**
  2. AJAX类库 0.9
  3. @appellation    AJAX Lib 0.9
  4. @author         KinM
  5. @date           2008-08-08
  6. @copyright      KinM
  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.             try
  57.             {
  58.                 this.XMLHttpReq = new ActiveXObject("Msxml4.XMLHTTP");
  59.             }
  60.             catch(e)
  61.             {
  62.                 try
  63.                 {
  64.                     this.XMLHttpReq = new ActiveXObject("Msxml3.XMLHTTP");
  65.                 }
  66.                 catch(e)
  67.                 {
  68.                     try
  69.                     {
  70.                         this.XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
  71.                     }
  72.                     catch(e)
  73.                     {
  74.                         try
  75.                         {
  76.                             this.XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  77.                         } 
  78.                         catch(oc)
  79.                         {
  80.                             this.failed=true;   //创建AJAX对象发生异常
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.             try
  86.             {
  87.                 //使用AcitveXObject函数创建浏览器
  88.                 this.XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
  89.             }
  90.             catch (e)
  91.             {
  92.                 //如果出现异常,再次尝试以如下方式创建XMLHttpRequest对象
  93.                 try
  94.                 {
  95.                     this.XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  96.                 }
  97.                 catch (e)
  98.                 {
  99.                     this.failed=true;   //创建AJAX对象发生异常
  100.                 }
  101.             }
  102.         }
  103.     };
  104.     
  105.     /**
  106.     发送请求函数
  107.     @param data 发送的数据
  108.     @example send("id=1");
  109.     */
  110.     this.Send=function(data)
  111.     {
  112.         var self=this;
  113.         //通过open方法取得与服务器的连接
  114.         if(this.method=="post")
  115.         {
  116.             this.XMLHttpReq.open(self.method,self.URLString,true);
  117.         }
  118.         else
  119.         {
  120.             this.XMLHttpReq.open(self.method,self.URLString+"?"+encodeURI(data),true);
  121.         }
  122.         //添加消息响应头
  123.         this.XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  124.         
  125.         //异步回调函数
  126.         this.XMLHttpReq.onreadystatechange = function()
  127.         {
  128.             //对象未创建
  129.             if (self.failed) {
  130.                 self.onFail();
  131.                 return;
  132.             }
  133.             
  134.             //消息响应标志
  135.             switch (self.XMLHttpReq.readyState) {
  136.                 case 1:
  137.                 {
  138.                     self.onLoading();
  139.                     break;
  140.                 }
  141.                 case 2:
  142.                 {
  143.                     self.onLoaded();
  144.                     break;
  145.                 }
  146.                 case 3:
  147.                 {
  148.                     self.onInteractive();
  149.                     break;
  150.                 }
  151.                 case 4:
  152.                 {
  153.                     if(self.XMLHttpReq.status==200) { 
  154.                         self.response = self.XMLHttpReq.responseText;
  155.                         self.responseXML = self.XMLHttpReq.responseXML;
  156.                         self.onCompletion();
  157.                     }
  158.                     else 
  159.                     { 
  160.                         self.onError();     //执行错误函数
  161.                     }
  162.                     break;
  163.                 }
  164.             }
  165.         };
  166.         
  167.         
  168.         if(this.method=="post")
  169.         {
  170.             this.XMLHttpReq.send(encodeURI(data)); //发送请求
  171.         }
  172.         else
  173.         {
  174.             this.XMLHttpReq.send(); //发送请求
  175.         }
  176.     };
  177.     
  178.     this.Abort=function()
  179.     {
  180.         this.XMLHttpReq.abort();
  181.     }
  182.     
  183.     this.Close=function()
  184.     {
  185.         this.XMLHttpReq=null;
  186.     }
  187.     //初始化AJAX库
  188.     this.Init();
  189. }

前台页面调用:

  1. <head runat="server">
  2.     <title>测试文件</title>
  3.         <script language="javascript" src="Ajax.js" type="text/javascript"></script>
  4.         <script language="javascript" src="UrlParameter.js" type="text/javascript"></script>
  5.     <script language="javascript" type="text/javascript">
  6.     var url=document.location.href;
  7.     var id=UrlParameter(url,"id");
  8.     if(id=="")
  9.          alert('参数为空');
  10.     else 
  11.        ajaxTest(id); 
  12.     function ajaxTest(id){
  13.         var st=new AJAXLib();
  14.         st.URLString="bb.aspx";
  15.         //st.method="post"
  16.         st.onCompletion=function()
  17.         {
  18.             alert(st.response);
  19.         }
  20.         st.Send("id="+id);
  21.         }
  22.         
  23.     </script>
  24. </head>

执行页面的后台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bb.aspx.cs" Inherits="pp_bb" %>

 

  1.     protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         string id;
  4.         id = Request.Form["id"];
  5.         string strIp = Request.UserHostAddress;
  6.         Response.Write("你的ID是:" + id + ",IP是" + strIp);
  7.         // Response.Write("pp");
  8.     }

这样就可以简单的实现,通过获取调用页的ID而在执行页进行处理,再返回信息!

原创粉丝点击