在项目中遇到纯js环境,不能用Jquery,因此产生以下纯JS 的Ajax和Action后台进行交互代码,可以写在公共js类中,以供调用。
function getObjXMLReq(){
var objXMLReq;
if (window.ActiveXObject && !(navigator.userAgent.indexOf('Mac') >= 0 && navigator.userAgent.indexOf("MSIE") >= 0)){
objXMLReq = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest){
objXMLReq = new XMLHttpRequest();
}
return objXMLReq;
}
function proxyRequest(action, method, parameter,isAsynchronism) {
var objXMLReq = getObjXMLReq();
var strURL = action + ".do";
var flag = 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);
var strResult = objXMLReq.responseText;
return strResult;
}
function getObjXMLReq(){
var objXMLReq;
if (window.ActiveXObject && !(navigator.userAgent.indexOf('Mac') >= 0 && navigator.userAgent.indexOf("MSIE") >= 0)){
objXMLReq = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest){
objXMLReq = new XMLHttpRequest();
}
return objXMLReq;
}
function proxyRequest(action, method, parameter,isAsynchronism) {
var objXMLReq = getObjXMLReq();
var strURL = action + ".do";
var flag = 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);
var strResult = objXMLReq.responseText;
return strResult;
}