An encapsulated Ajax class

Source: Internet
Author: User

Usage:
New Ajax (). Request (url, cmd, async, method, postString, title)
Parameters:
Url: URL of the Request page (required)
Cmd: return value processing function (required)
Async: asynchronous or not, (true | false). The default value is true.
Method: Request method, (post | get), default get
PostString: When the request method is post, the request content
Title: request content title
Copy codeThe Code is as follows:
// Ajax encapsulation 2007-3-13
Function createXMLHttpRequest (){
Try {
If (window. XMLHTTPRequest ){
Return new XMLHttpRequest ();
}
Else if (window. ActiveXObject ){
Return new ActiveXObject ("Microsoft. XMLHTTP ");
}
}
Catch (e) {alert ("XMLHttpRequest object cannot be created! Check IE security settings! ");}
}
Function messageDiv (t)
{
Var v = document. createElement ("<div> ");
V. innerHTML = "<table style = \" width: 300px; \ "id = message>" +
"<Tr style = \" font-size: 12px; background-color: # eeeeeeff; color: #227933; height: 20px \ ">" +
"<Td style = \" padding: 2px; border-top: 1px solid # E1E1E1; border-left: 1px solid # E1E1E1; border-bottom: 1px solid #818181; border-right: 1px solid # A1A1A1 \ ">" +
"<Nobr> the connection is not initialized... </span> </nobr> </td> </tr> </table> ";
Var l = document. getElementsByName ("message"). length;
V.style.css Text = "position: absolute; bottom:" + (l * 24) + "px; left: 0px; display: none ";
Document. body. appendChild (v );
This. clear = function (){
Document. body. removeChild (v );
Var msg = document. getElementsByName ("message ");
For (var I = 0; I <msg. length; I ++ ){
Msgpolici}.parentnode.style.css Text = "position: absolute; bottom:" + (I * 24) + "px; left: 0px ";
}
}
This. showmsg = function (s ){
V. style. display = "";
V. all. Span1.innerHTML = s;
}
}
Function Ajax (){
Var x = new createXMLHttpRequest ();
This. Request = function (url, cmd, async, method, postString, title ){
If (method! = "Post") method = "post"; else method = "get ";
If (async! = True) async = true; else async = false;
If (typeof (postString )! = "String") postString = "";
If (typeof (title )! = "String") title = "getting data"; else title = "getting" + title;
Var msgbox = new messageDiv (title );
X. onreadystatechange = function ()
{
If (async) switch (x. readystate ){
Case 1:
Msgbox. showmsg ("Initializing connection ...");
Return;
Case 2:
Msgbox. showmsg ("sending data ...");
Return;
Case 3:
Msgbox. showmsg ("receiving data ...");
Return;
Case 4:
Msgbox. showmsg ("data reception completed ...");
If (x. status = 200 ){
Cmd (x. responseText );
Msgbox. clear ();
}
Else {
Msgbox. showmsg ("request failed," + x. statustext + "(" + x. status + ")");
SetTimeout (msgbox. clear, 3000 );
}
Return;
}
}
X. open (method, url, async );
If (method = "post") {msgbox. showmsg ("receiving data..."); x. send (postString) ;}else x. send ();
If (! Async ){
Msgbox. showmsg ("data reception completed ...");
Cmd (x. responseText );
Msgbox. clear ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.