/**********************************************
*@authorskyz
*@functionjavascriptclientajaxdealwith
*@datetime2006-3-20
**********************************************
*Function:CreateahttpRequestobject
**********************************************/
functionHttpRequest(){
this._httpRequest=null;//HttpRequestrequestobject
this._callBack=null;//Callbackfunction
this._domResult=true;//Resultifdomobjectortextstring
this._requestData=null;//Requestdata
this._requestResult=null;//HttpRequestresult
this._stateString=null;//Currentrequeststatestring
this._error=false;//Currentifhaveerror
this._callBackPara=null;//Currentcallbackfunctionparama
//internalmethodforgetHttpRequestObject
this.init=function(){
//JudgeifNotIE
if(window.XMLHttpRequest){
this._httpRequest=newXMLHttpRequest();
//Setrequestmimeistext/xml
if(this._httpRequest.overrideMimeType){
this._httpRequest.overrideMimeType('text/xml');
}
}elseif(window.ActiveXObject){
try{
this._httpRequest=newActiveXObject("Msxml2.XMLHTTP");
}catch(ex){
try{
this._httpRequest=newActiveXObject("Microsoft.XMLHTTP");
}catch(ex){
this._setMessage(ex,true);
return;
}
}
//this._httpRequest.setrequestheader("Content-Type","text/xml;charset=gb2312");
}
//JudgeHttpRequestobjectcreatesuccessful
if(!this._httpRequest){
this._setMessage("XMLHttpRequest对象创建失败!请重试......",true);
return;
}
}
/*
*Function:Settherequestheader
*namePar:request'sheadername
*valuePar:request'sheadervalue
*/
this.doSetRequestHeader=function(namePar,valuePar){
if(this._error){
return;
}
this._httpRequest.setRequestHeader(namePar,valuePar);
}
/*
*Function:Settherequestdata
*dataPar:request'ssenddata;
*/
this.doSetRequestData=function(dataPar){
if(this._error){
return;
}
this._requestData=dataPar;
}
/*
*FunctiongetRequestHttpObject
*/
this._getRequestObj=function(){
if(this._error){
return;
}
returnthis._httpRequest;
}
/*
*Function:SetCallbackfunctionpara
*/
this.doSetCallBack=function(callBack,paraData){
this._callBack=(callBack)?callBack:null;
this._callBackPara=(paraData)?paraData:null;
};
/*
*Function:GetcurrentstateString
*/
this.doGetState=function(){
returnthis._stateString;
}
/*
*Function:getcurrentError
*/
this.doGetError=function(){
returnthis._error;
}
/*
*
*/
this.doCallBack=function(){
this._callBack(this._requestResult,this._callBackPara);
}
/*
*Function:Sendtherequest
*urlPar:request'surlpath
*[methodPar]:request'smethod
*[domPar]:request'sresultisdomorstring
*/
this.doSendResuest=function(urlPar,methodPar,obj,domPar,asyPar){
if(obj._error){
return;
}
methodPar=((methodPar)?methodPar:"GET");
asyPar=((asyPar)?asyPar:true);
this._domResult=(domPar)?domPar:obj._domResult;
try{
vara=this._getRequestObj();
a.onreadystatechange=function(){
if(obj._error){
return;
}
varreadyStateTmp=a.readyState;
if(readyStateTmp==0){
obj._setMessage("未初始化!");
}elseif(readyStateTmp==1){
obj._setMessage("正在读取中......");
}elseif(readyStateTmp==2){
obj._setMessage("已经读取过!");
}elseif(readyStateTmp==3){
obj._setMessage("正在逐个切换......");
}elseif(readyStateTmp==4){
varstatusTmp=a.status;
if(statusTmp==404){
obj._setMessage("未找到请求页面!",true);
}elseif(window.location.href.indexOf("http")==-1||statusTmp==200){
obj._setMessage("完成!");
if(this._domResult&&window.XMLHttpRequest){
obj._requestResult=a.responseXml;
}else{
obj._requestResult=a.responseText;
}
if(obj._callBack){
obj.doCallBack();
}
}else{
obj._setMessage("未知错误!");
}
}else{
obj._setMessage("未知错误!");
}
}
a.open(methodPar,urlPar,asyPar);
//a.setRequestHeader("If-Modified-Since","0");
a.send(obj._requestData);
}catch(ex){
obj._setMessage(ex,true);
}
}
/*
*Function:Dealexceptionerror
*exPar:errorstring
*/
this._setMessage=function(exPar,mark){
this._stateString=exPar.toString();
this._error=(mark)?mark:false;
}
}
本地下载