国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武汉市| 明溪县| 肥城市| 扶沟县| 平阴县| 江山市| 子洲县| 徐水县| 武川县| 盐城市| 广宁县| 漳浦县| 佳木斯市| 洛阳市| 浮山县| 临泽县| 远安县| 安庆市| 县级市| 常德市| 沙田区| 博爱县| 环江| 灵璧县| 安岳县| 辽阳市| 信阳市| 宁南县| 张家界市| 呼伦贝尔市| 米林县| 齐河县| 吴旗县| 洛隆县| 九龙坡区| 淅川县| 且末县| 莲花县| 台中县| 石城县| 新郑市|