Ajax asynchronous (request) submission class supports cross domain

Time:2022-1-27
Copy codeThe code is as follows:
/**//*
Asynchronous request class

Author: I am not unintentional
Created on: February 2009
———————————————————————————————————————————

Modification record:

2009.4.27 – Add URL detection function, if it is “http: / / XXXX. XXX. XX. XX /.” This format is obtained by using “/ geturl. ASPX” provided by the system
To solve cross domain problems.
getUrl. There are two main parameters of ASPX: 1. The URL value is the target URL (parameters can be included in the URL) – required; 2. The method value is the method used by the agent (optional)
If the method is empty, the post method is used for proxy by default; If the form parameter is empty (i.e. request. Content length = 0), it is automatically set to the get method;
(Note: geturl.aspx was redesigned on April 27, 2009, and a cookie proxy was added on the basis of the original proxy.)
*/
function IsOuterURL(str_url){
var strRegex = “((https|http)://)([\\w-]+\\.)+[\\w-]+.([^a-z])(/[\\w-./?%&=]*)?|((https|http)://)[a-zA-Z0-9\\-\\.][\\w-]+.([^a-z])(/[\\w-./?%&=]*)?”;
var re=new RegExp(strRegex);

if (re.test(str_url)){
return (true);
}else{
return (false);
}
}
var LeesAjaxRequest=
{
list:[],
m_LeesAjaxHttpRequest:null,
isWorking:false,
getUrl:function(method,url,params,successFun,FailureFun,headers)
{
if(url.length<1)
return;
if(this.m_LeesAjaxHttpRequest==null)
{
if (typeof XMLHttpRequest != ‘undefined’)
{
this.m_LeesAjaxHttpRequest = new XMLHttpRequest();
}
else if (typeof ActiveXObject != ‘undefined’)
{
this.m_LeesAjaxHttpRequest = new ActiveXObject(‘Microsoft.XMLHTTP’);
}

}
if (this.m_LeesAjaxHttpRequest)
{
if(this.isWorking)
{
this.list[this.list.length]={method:method,url:url,params:params,successFun:successFun,failureFun:FailureFun,headers:headers};
}
else
{
this.isWorking=true;
if(IsOuterURL(url))
this. m_ LeesAjaxHttpRequest. open(method, “/geturl.aspx?url=”+escape(url)+”&method=”+method, true);// True is asynchronous
else
this. m_ LeesAjaxHttpRequest. open(method, url, true);// True is asynchronous
var _this=this;
this.m_LeesAjaxHttpRequest.onreadystatechange=function()
{
if(4==_this.m_LeesAjaxHttpRequest.readyState)
{
if(200==_this.m_LeesAjaxHttpRequest.status)
{
if(successFun)
{
try{
successFun(_this.m_LeesAjaxHttpRequest);
}
catch(ex)
{}
}
}
else
{
if(FailureFun)
{
try{
FailureFun(_this.m_LeesAjaxHttpRequest);
}
catch(ex)
{}
}
}
_this.isWorking=false;
if(_this.list.length>0)
{
var o=_this.list[0];
_this.list.splice(0,1);
_this.getUrl(o.method,o.url,o.params,o.successFun,o.failureFun,o.headers);
}
}
};
var vPara=””;
if(typeof params==”string”)
{
vPara=escape(params);
}
else if(params)
{
try
{
for(var e in params)
{
if(vPara.length<1)
vPara=e+”=”+escape(params[e]);
else
vPara+=”&”+e+”=”+escape(params[e]);
}
}
catch(ex)
{}
}
if(headers)
{
try
{
for(var h in headers)
{
this.m_LeesAjaxHttpRequest.setRequestHeader(h.replace(“_”,”-“),headers[h]);
}
}
catch(ex)
{
}
}
this.m_LeesAjaxHttpRequest.send(vPara);
}
}
},
Post:function(o)
{
var vMethod=o.method||”post”;
var vUrl=o.url||null;
var vParams=o.params||””;
var vSuccess=o.success||null;
var vFailure=o.failure||null;
var vHeaders=o.headers||null;
if(vUrl==null||vUrl.length<1)
{
//Alert (“asynchronous request format error”);
return;
}
this.getUrl(vMethod,vUrl,vParams,vSuccess,vFailure,vHeaders);
},
Get:function(o)
{
var vMethod=o.method||”get”;
var vUrl=o.url||null;
var vParams=o.params||””;
var vSuccess=o.success||null;
var vFailure=o.failure||null;
var vHeaders=o.headers||null;
if(vUrl==null||vUrl.length<1)
{
//Alert (“asynchronous request format error”);
return;
}
this.getUrl(vMethod,vUrl,vParams,vSuccess,vFailure,vHeaders);
}
}
/**//*Asynchronous request class end*/

Use example:

Copy codeThe code is as follows:
1. / / dynamically load JS files
if(vJS && vJS.length>3)
{
if(!_this.loadedJS.isInArray(vJS))
{
_this.loadedJS[_this.loadedJS.length]=vJS;
LeesAjaxRequest.Get({
url:vJS,
success:function(ojs){
var jsOBJ=document.createElement(“script”);
jsOBJ.text=ojs.responseText;
document.documentElement.appendChild(jsOBJ);
},//end success
headers:{Content_Type:”application/x-javascript”}
});//end Get
}//end if ( !_this.loadedJS.isInArray(vJS))
}//end if(vJS && vJS.length>3)

2 .// Add to tooltip bar
LeesAjaxRequest.Post({url:”/getToolTip.aspx”,
params:{tooltip:title},
Headers: {content_type: “application / x-www-form-urlencoded; charset = UTF-8”}, / / if the post method is used, this parameter must be passed in. Charset can be anything else
success:function(o){
var rObj=eval(“(“+o.responseText+”)”);
var tObj=new LeesBaseWindow(rObj[“width”]+2,rObj[“height”]+1,””,””,””,””,””,1,””);
tObj.ShowWindow(vToolBarSlider.window);
tObj.contentWindow.style.backgroundImage=”url(“+rObj[“image”]+”)”;
tObj.contentWindow.style.marginLeft=”1px”;
tObj.contentWindow.style.backgroundRepeat=”no-repeat”;
tObj.contentWindow.style.backgroundPosition=”0 0″;
tObj.contentWindow.onmouseover=function(){
this.style.backgroundPosition=”0 -“+rObj.height;
_this.setShowWindow(obj);
}
tObj.contentWindow.onmouseout=function(){
this.style.backgroundPosition=”0 0″;
}
},
failure:function(){
Alert (“error generating tooltip”);
}
});