Copy Code code as follows:
/*
Call Mode:
1.POST mode
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) {//server gives a response
if (Xmlhttp.status = 200) {//service correct response
alert (Xmlhttp.responsetext);
}
XmlHttp = null; Recycle resources
}
}
, "Data": Data
};
Ajax (option);
2.GET mode
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) {//server gives a response
if (Xmlhttp.status = 200) {//service correct response
alert (Xmlhttp.responsetext);
}
XmlHttp = null; Recycle resources
}
}
};
Ajax (option);
*/
function Ajax (option) {
Createxmlhttprequest (); Creating XMLHttpRequest Objects
if (option!= null && option!= undefined) {
if (Option.url = = NULL && Option.url = = undefined) {
XmlHttp = null;
Alert ("Lack of necessary parameter Option.url");
Return
}
if (option.action = = NULL && option.action = = undefined) {
XmlHttp = null;
Alert ("Lack of necessary parameter option.action");
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 recycle the xmlHttp = null after the call completes;
/* 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 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;
}