国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > AJAX > 正文

給初學ajax的人 ajax函數代碼

2024-09-01 08:29:17
字體:
來源:轉載
供稿:網友
代碼如下:
/*
調用方式:
1.POST方式
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) {//服務器給了回應
if (xmlHttp.status == 200) {//服務正確響應
alert(xmlHttp.responseText);
}
xmlHttp = null; //回收資源
}
   }
, "data": data
};
ajax(option);
2.GET方式
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) {//服務器給了回應
if (xmlHttp.status == 200) {//服務正確響應
alert(xmlHttp.responseText);
}
xmlHttp = null; //回收資源
}
   }
};
ajax(option);
*/
function ajax(option) {
createXMlHttpRequest(); //創建xmlHttpRequest 對象
if (option != null && option != undefined) {
if (option.url == null && option.url == undefined) {
xmlHttp = null;
alert("缺少必要參數option.url");
return;
}
if (option.action == null && option.action == undefined) {
xmlHttp = null;
alert("缺少必要參數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; //調用完成后最好回收下 xmlHttp = null;
/*獲取元素*/
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;
}
}
/*創建ajax請求對象*/
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;
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吕梁市| 穆棱市| 聂拉木县| 德江县| 临泉县| 策勒县| 监利县| 突泉县| 上犹县| 崇阳县| 阿坝县| 布尔津县| 资溪县| 南召县| 铜鼓县| 宣城市| 辽宁省| 弋阳县| 诏安县| 肥城市| 上饶市| 武定县| 淮滨县| 红桥区| 平利县| 淄博市| 溆浦县| 忻州市| 保靖县| 团风县| 云安县| 康保县| 张家港市| 阿尔山市| 玉环县| 温州市| 海口市| 阳城县| 富宁县| 梅河口市| 武山县|