Also write an Ajax Request class with code

Time:2022-5-6

Purpose: because some modules in the blog program need Ajax, use prototype directly JS has a large volume (more than 40 K) and only uses the Ajax function. Therefore, in order to reduce the burden of downloading, it cannot be changed The code written under JS framework can only write an Ajax class according to the style of prototype to achieve zero cost transplantation framework.

The new Ajax classes are as follows:

var Ajax = {xmlhttp:function(){
try{
return new ActiveXObject(‘Msxml2.XMLHTTP’);
}catch(e){
try{
return new ActiveXObject(‘Microsoft.XMLHTTP’);
}catch(e){
return new XMLHttpRequest();
}

}
};

Ajax.Request = function(){
if (arguments.length<2) return;
var _p = {asynchronous:true,method:”GET”,parameters:””}; //default option
for (var key in arguments[1]){ // custom option overwrite default option
_p[key] = arguments[1][key]; 
}
var _x = Ajax.xmlhttp(); //xml obj
var _url = arguments[0]; //str
if(_p[“parameters”].length>0) _p[“parameters”] += ‘&_=’;
if(_p[“method”].toUpperCase()==”GET”)_url += (_url.match(/\?/) ? ‘&’ : ‘?’) + _p[“parameters”];
_x.open(_p[“method”],_url,_p[“asynchronous”]);
_x.onreadystatechange = function(){
if (_x.readyState==4){
if(_x.status==200){
_p[“onComplete”]?_p[“onComplete”](_x):””;
}else{
_p[“onError”]?_p[“onError”](_x):””;
}
}
}
if(_p[“method”].toUpperCase()==”POST”)_x.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);
_x.send(_p[“method”].toUpperCase()==”POST” ? _p[“parameters”] : null);
};

This class is saved as a JS file with a volume of less than 1K, which is small enough. ha-ha.
Call method:

var myAjax = new Ajax.Request(
“http://localhost/abc.asp”,
{
method:”post”,
parameters:”demo=123456789abc”,
onComplete:function(xmlhttp){
alert(xmlhttp.responseText)
}
}
);

The calling style is exactly the same as the original!

At present, this new class has only two callback functions: oncomplete # and # onerror. Ajax class also has only one method of request. After all, blog programs don’t need so many applications. The parameters} attribute has a default value: {asynchronous: true, method: “get”, parameters: “}. You can know from it that if the three parameters of asynchronous, method and parameters are not passed in during the call, the class will use the default value.

Recommended Today

How does ArcGIS translate graphics? Skills of translating vector data in ArcGIS

If the graphics drawn in ArcGIS want to be translated, how to translate the graphics? Let’s take a look at the detailed tutorial. ArcGIS desktop 10.7 perfect Chinese Special Edition (with activation patch + Chinese package + installation tutorial) Type:Programming tools size:1.22GB Language:Simplified Chinese Time:2020-07-22 View details Open ArcGIS and add layers. Right clickClick layer, […]