CopyCode The Code is as follows :/*
Call method:
1. Post Method
VaR TXT = escape (sender. Value); // document. getelementbyid ("<% = txtname. clientid %>"). value );
VaR DATA = "name =" + TXT + "& Pwd =" + txt;
VaR option = {"url": "handler/handler. ashx"
, "Action": "Post"
, "Callback": function (){
If (XMLHTTP. readystate = 4) {// the server sends a response
If (XMLHTTP. Status = 200) {// The service responds correctly
Alert (XMLHTTP. responsetext );
}
XMLHTTP = NULL; // reclaim Resources
}
}
, "Data": Data
};
Ajax (option );
2. Get Method
VaR TXT = escape (sender. Value); // document. getelementbyid ("<% = txtname. clientid %>"). value );
VaR option = {"url": "handler/handler. ashx & name =" + TXT + "& Pwd =" + txt
, "Action": "Post"
, "Callback": function (){
If (XMLHTTP. readystate = 4) {// the server sends a response
If (XMLHTTP. Status = 200) {// The service responds correctly
Alert (XMLHTTP. responsetext );
}
XMLHTTP = NULL; // reclaim Resources
}
}
};
Ajax (option );
*/
Function Ajax (option ){
Createxmlhttprequest (); // create an XMLHTTPRequest object
If (option! = NULL & option! = Undefined ){
If (option. url = NULL & option. url = undefined ){
XMLHTTP = NULL;
Alert ("the required parameter option. url is missing ");
Return;
}
If (option. Action = NULL & option. Action = undefined ){
XMLHTTP = NULL;
Alert ("the required parameter option. Action is missing ");
Return;
}
XMLHTTP. Open (option. Action, option. url, true );
If (option. contenttype! = NULL & option. contenttype! = Undefined ){
XMLHTTP. setRequestHeader ("Content-Type", option. contenttype );
} Else {
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
}
If (option. callback! = NULL & option. callback! = Undefined ){
XMLHTTP. onreadystatechange = option. Callback;
}
If (option. Action. touppercase () = "Post "){
XMLHTTP. Send (option. data );
} Else {
XMLHTTP. Send (null );
}
}
}
VaR XMLHTTP; // it is best to reclaim XMLHTTP = NULL after the call is complete;
/* Get element */
Function g (ARG ){
VaR T = Document. getelementbyid (ARG );
If (null! = T & T! = Undefined ){
Return T;
}
T = Document. getelementsbyname (ARG );
If (null! = T & T! = Undefined ){
Return T;
}
T = Document. getelementsbytagname (ARG );
If (null! = T & T! = Undefined ){
Return T;
}
}
/* Create an Ajax request object */
Function createxmlhttprequest (){
Try {// Firefox, chrome, surfri, opera + 8
XMLHTTP = new XMLHttpRequest ();
}
Catch (IE ){
Try {// IE6 +
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
} Catch (IE ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
}
Return XMLHTTP;
}