简化AJAX操作的javascript类(来自锐锋,使用请注明出处)

// JScript 文件
//来自锐锋,使用请注明出处
//创建ajax请求的函数,本函数可跨平台使用
function createAjaxObj()
{
    var httprequest=false;
    if(window.XMLHttpRequest)
    {
        httprequest = new XMLHttpRequest();
        if(httprequest.overrideMimeType)
        {
            httprequest.overrideMimeType("text/xml");
        }
    }
    else if(window.ActiveXObject)
    {
        try
        {
            httprequest = new ActiveXObject(Msxml2.XMLHTTP);
        }
        catch (e)
        {
            try
            {
                httprequest=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){}
        }
    }
    return httprequest;
}
//类核心部分
//url:要请求的地址
//isNewp:true可禁止服务器缓冲机制,做聊天程序时必须使用true
/*
使用实例
var myajax = new AjaxRequest("dealBusiness.jsp",true)
myajax.addPara("newPara","value");
myajax.requestType = "get";//可有可无
myajax.dealrequest= function()//处理事件
{
     if(myajax.ART.readyState==4)
     {
          alert(myajax.ART.responseText);
     }
}
myajax.startRequest();
*/
function AjaxRequest(url,isNewp)
{
   
 var paraList = "";
 var requestS = false;
 
 //public
 this.requestType = "GET";
 this.submitURL = url;
 this.isNew = isNewp;
 this.returnText = "";
 this.ART = createAjaxObj();
 
    this.addPara = function(paraName,paraValue)
    {
        if(paraList=="")
  {
   paraList = paraName +"="+paraValue;
  }
  else
  {
   paraList += "&"+paraName +"="+paraValue;
  }
    }
   
    this.startRequest = function()
    {
        if(this.ART.readyState==4||this.ART.readyState==0)
  {
   this.ART = createAjaxObj();
   if(this.requestType=="GET"||this.requestType=="Get"||this.requestType=="get")
   {
       if(this.isNew)
    {
     this.ART.open("GET",this.submitURL+"?"+paraList+"&newTime="+new Date().getTime(),true);
    }
    else
    {
     this.ART.open("GET",this.submitURL+"?"+paraList,true);
    }
   }
   else
   {
       if(this.isNew)
    {//+"?"+paraList+"&newTime="+new Date().getTime()
     this.ART.open("POST",this.submitURL,true);
    }
    else
    {//+"?"+paraList
     this.ART.open("POST",this.submitURL,true);
    }
    this.ART.setRequestHeader("Content-type","application/x-www-form-urlencoded");
   }
    //alert(this.submitURL+"?"+paraList+"&newTime="+new Date().getTime());
  
   this.ART.onreadystatechange = this.dealrequest;
  
   if(this.requestType=="GET"||this.requestType=="Get"||this.requestType=="get")
   {
       this.ART.send(null);
      }
      else
      {
          if(this.isNew)
    {
     this.ART.send(paraList+"&newTime="+new Date().getTime());
    }
    else
    {
     this.ART.send(paraList);
    }
      }
  }
    }
    this.dealrequest=function(){}
}
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值