AJAXPro(根据AJAX原理,自写的JS文件)

// JavaScript Document
//AjaxPro
// 声明 var objectname = new AjaxPro(div,value)
//参数 --[div]-->要显示数据的标签;[value]-->POST方法中要传递的值;[httpurl]-->AJAX指向的后台页面路径
//使用时声明对象后,直接可以用对象的方法,Get方法用于URL传值,Post方法用于提交数据
//使用时请标名出处
function AjaxPro(object)
{
       var XMLhttp=Init();
       var Float = true;
       var ids = document.getElementById(object);
       function Init()
       {
              var xmlhttp;
              try
              {
                     xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch (e)
              {
                     try
                     {
                            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                     }
                     catch (e)
                     {
                            xmlhttp = false;
                     }
              }
              if (!xmlhttp && typeof XMLHttpRequest!='undefined')
              {
                     try
                     {
                            xmlhttp = new XMLHttpRequest();
                     }
                     catch (e)
                     {
                            xmlhttp=false;
                     }
              }
              if (!xmlhttp && window.createRequest)
              {
                     try
                     {
                            xmlhttp = window.createRequest();
                     }
                     catch (e)
                     {
                            xmlhttp=false;
                     }
              }
              return xmlhttp;
       }
       this.Load = function(method,HttpUrl,value)
       {
              if(value==null){}
              XMLhttp.open(method,HttpUrl,Float);
              XMLhttp.onreadystatechange = this.Process;
              var https=null;
              if(method=="Post")
              {
                     XMLhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                     https = value;
              }
              XMLhttp.send(https);
       }
 
       this.Process = function() //发送请求,获取数据
       {
              if(XMLhttp.readyState == 4)
              {
                     if(XMLhttp.status == 200)
                     {
                            ids.innerHTML="";
                            ids.innerHTML=XMLhttp.responseText;
                            XMLhttp.abort();
                      }
                      else
                     {
                            ids.innerHTML = "HTTP 错误,状态码:" + XMLhttp.status;
                            XMLhttp.abort();
                     }
              }
       }
 
       this.Get = function(HttpUrl)
       {
              if(HttpUrl.indexOf("?")==-1)
              {
                    this.Load("Get",HttpUrl+"?rndmath="+Math.random());
              }
              else
              {
                    this.Load("Get",HttpUrl+"&rndmath="+Math.random());
              }
       }

       this.Post=function(HttpUrl,value)//Post数据
       {
             this.Load("Post",HttpUrl,value);
       }
 
<html >
<head id="Head1" runat="server">
     <title>AJAXPro测试</title>
     <script language="javascript" type="text/javascript" src="AjaxPro.js"></script>
     <script language="javascript" type="text/javascript">
          <!--
           function checkLeave()
           {
                 var Ajax = new AjaxPro("d01");
                 Ajax.Get2("AjaxUnlock.aspx");
       }
          -->
       </script>
</head>
<body>
    <form id="form1" runat="server">
        <div id="d01">
        </div>
     </form>
</body>
</html>
 
Ajax.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax.aspx.cs" Inherits="AjaxUnlock" %>
 
Ajax.aspx.cs
public partial class Ajax: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string Result="测试AJAXPro!";
            Response.Write(Result);
            Response.End();
        }        
    }
}
注意:被调用的Ajax.aspx页面一定要把除<%@ Page ……%>以外的其他代码全部删除
posted @ 2009-04-14 22:40    阅读(653)  评论(1编辑  收藏  举报