您的位置:首页 > Web前端 > JavaScript

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

2007-06-19 16:39 323 查看
// 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(){}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: