Copy codeThe 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;
}