xmlHttpRequest封装类

xmlHttp.js:

 var Request = new Object();
Request.reqList=[];
function getAjax()
{
    var ajax=false;
    try
    {
        ajax=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            ajax=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(E)
        {
            ajax=false;
        }
    }
    if(!ajax && typeof XMLHttpRequest!="undefined")
    {
        ajax=new XMLHttpRequest();
    }
    return ajax;
}

Request.send=function(url,method,callback,data,urlencoded,callback2)
{
    var req=getAjax();
    req.onreadystatechange=function()
    {
        if(req.readyState==4)
        {
            if(req.status<400)
            {
                if(callback)callback(req,data);
            }
            else   
            {
                alert("加載數據時發生錯誤:/n"+req.status+"/"+req.statusText);
                if(callback2)
                {
                    callback2(req,data);
                }
             }
             //afterLoading();
             try
             {
                delete req;
                req=null;
             }
             catch(e)
             {
             }
        }
    }
    if(method=="POST")
    {
        req.open("POST",url,true);
        if(urlencoded)
        {
            req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        }
        req.send(data);
        Request.reqList.push(req);
    }
    else
    {
        req.open("GET",url,true);
        req.send(null);
        Request.reqList.push(req);
    }
       
    //loading();
    return req;
}
   
Request.clearReqList = function()
{
    var ln=Request.reqList.length;
    for(var i=0;i<ln;i++)
    {
        var req = Request.reqList[i];
        if(req)
        {
            try
            {
                delete req;
            }
            catch(e)
            {}
         }
    }
    Request.reqList =[];
}

Request.sendPOST=function(url,method,callback,data,clear,callback2)
{
    if(clear)
    {
        Request.clearReqList();
    }
    Request.send(url,"POST",callback,data,true,callback2);
}

Request.sendGET=function(url,method,callback,args,clear,callback2)
{
    if(clear)
    {
        Request.clearReqList();
    }
    Request.send(url,"GET",callback,args,false,callback2);
}
  
//   function loading()
//   {
//        var div = $("loadingflag");
//        alert(div);
//        div.style.display="";
//   }
//  
//   function afterLoading()
//   {
//        var div = $("loadingflag");
//        div.style.display="none";
//   }
  
   function $()
   {
        var elements = new Array();
        for (var i = 0; i < arguments.length; i++)
        {
            var element = arguments[i];
            if (typeof element == 'string')
            {
                element = document.getElementById(element);
            }
            if (arguments.length == 1)
                return element;
            elements.push(element);
        }
        return elements;
    }

 

请用示例:

function Send()
{
    Request.send("Handler.ashx?BillNO="+value,"GET",ReadInfor_callback,null,null,null);
}
function ReadInfor_callback(res)
{
    alert(res);
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值