Ajax function code for beginners

Time:2021-12-7
Copy codeThe code is as follows:
/*
Call method:
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) {/ / the server responded
If (xmlhttp. Status = = 200) {/ / the service responds correctly
alert(xmlHttp.responseText);
}
xmlHttp = null; // Recycling 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) {/ / the server responded
If (xmlhttp. Status = = 200) {/ / the service responds correctly
alert(xmlHttp.responseText);
}
xmlHttp = null; // Recycling resources
}
   }
};
ajax(option);
*/
function ajax(option) {
createXMlHttpRequest(); // Create XMLHttpRequest object
if (option != null && option != undefined) {
if (option.url == null && option.url == undefined) {
xmlHttp = null;
Alert (“missing required parameter option. URL”);
return;
}
if (option.action == null && option.action == undefined) {
xmlHttp = null;
Alert (“missing required 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; // After the call is completed, you’d better recycle xmlhttp = null;
/*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;
}

Recommended Today

Heavyweight Tencent cloud open source industry’s first etcd one-stop governance platform kstone

​ Kstone open source At the kubecon China Conference held by CNCF cloud native foundation on December 9, 2021,Tencent cloud container tke team released the open source project of kstone etcd governance platform. KstoneIt was initiated by the TKE team of Tencent cloud containerCloud native one-stop etcd governance project based on kubernetes。 The project originates […]