Ajax和后台Action类交互封装

在项目中遇到纯js环境,不能用Jquery,因此产生以下纯JS的Ajax和Action后台进行交互代码,可以写在公共js类中,以供调用。

functiongetObjXMLReq(){

varobjXMLReq;

if(window.ActiveXObject&&!(navigator.userAgent.indexOf('Mac')>=0&&navigator.userAgent.indexOf("MSIE")>=0)){

objXMLReq=newActiveXObject("Microsoft.XMLHTTP");

}

elseif(window.XMLHttpRequest){

objXMLReq=newXMLHttpRequest();

}

returnobjXMLReq;

}

functionproxyRequest(action,method,parameter,isAsynchronism){

varobjXMLReq=getObjXMLReq();

varstrURL=action+".do";

varflag=false;

if(method!=null&&method!=""){

strURL+="?method="+method;

flag=true;

}

if(parameter==null)parameter="";

if(flag)

strURL+="&isPartlyRefresh=true"+parameter;

else

strURL+="?isPartlyRefresh=true"+parameter;

if(!isAsynchronism)isAsynchronism=false;

objXMLReq.open("POST",strURL,isAsynchronism);

objXMLReq.send(parameter);

varstrResult=objXMLReq.responseText;

returnstrResult;

}

相关推荐