Basic usage:
CopyCode The Code is as follows: var Ajax = new ajaxobj (URL );
Ajax. addlistener (200, function (r ){
Alert (R );
});
Ajax. Send ();
It can also be called consecutively: Copy codeThe Code is as follows: var Ajax = new ajaxobj (URL). addlistener (200, function (r ){
Alert (R );
}). Send ();
You can also customize post or get requests and monitor different HTTP status codes.
Complete code: Copy code The Code is 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 );
};
};