Basic usage:
Copy Code code as follows:
var ajax = new Ajaxobj (URL);
Ajax.addlistener function (r) {
Alert (R);
});
Ajax.send ();
You can also call continuously:
Copy Code code as follows:
var ajax = new Ajaxobj (URL). AddListener function (r) {
Alert (R);
}). Send ();
It also supports custom post or get-mode requests, as well as monitoring different HTTP status codes, looking at the code for yourself:)
Complete code:
Copy Code code as follows:
ajaxobj = function (URL, method, content) {
THIS.R = null;
This.url = URL;
This.method = method;
this.content = content;
This.header = {};
this.header["Connection"] = "close";
this.header["Content-type"] = "application/x-www-form-urlencoded";
var self = this;
if (window. XMLHttpRequest) {
THIS.R = new XMLHttpRequest ();
}else if (window. ActiveXObject) {
try {
THIS.R = new ActiveXObject ("Msxml2.xmlhttp");
catch (e) {
try{
THIS.R = new ActiveXObject ("Microsoft.XMLHTTP");
catch (e) {
}
}
}
This.addlistener = function (Http_status, func) {
if (!this. L
This. L=[];
This. L[http_status] = func;
return this;
};
This.setheader = function (name, value) {
This.header[name] = value;
This.r.setrequestheader (name, value);
return this;
};
This.send = function () {
if (This.method!= "POST" && this.method!= "get")
This.method = "Get";
This.r.open (This.method, This.url, true);
for (Var h in This.header) {
This.r.setrequestheader (H, this.header[h]);
}
This.r.send (this.content);
};
if (THIS.R) This.r.onreadystatechange = function () {
if (self.r.readystate = = 4 && self. L[self.r.status]!= null)
Self. L[self.r.status] (Self.r.responsetext);
};
};