給初學(xué)ajax的人 ajax函數(shù)代碼
更新時間:2010年05月31日 20:23:44 作者:
是原生的ajax,稍稍的封裝了下,對了,option為json格式的數(shù)據(jù),對此可先看這個
復(fù)制代碼 代碼如下:
/*
調(diào)用方式:
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) {//服務(wù)器給了回應(yīng)
if (xmlHttp.status == 200) {//服務(wù)正確響應(yīng)
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) {//服務(wù)器給了回應(yīng)
if (xmlHttp.status == 200) {//服務(wù)正確響應(yīng)
alert(xmlHttp.responseText);
}
xmlHttp = null; //回收資源
}
}
};
ajax(option);
*/
function ajax(option) {
createXMlHttpRequest(); //創(chuàng)建xmlHttpRequest 對象
if (option != null && option != undefined) {
if (option.url == null && option.url == undefined) {
xmlHttp = null;
alert("缺少必要參數(shù)option.url");
return;
}
if (option.action == null && option.action == undefined) {
xmlHttp = null;
alert("缺少必要參數(shù)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; //調(diào)用完成后最好回收下 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;
}
}
/*創(chuàng)建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;
}
相關(guān)文章
使用wordpress的$wpdb類讀mysql數(shù)據(jù)庫做ajax時出現(xiàn)的問題該如何解決
這篇文章主要介紹了使用wordpress的$wpdb類讀mysql數(shù)據(jù)庫做ajax時出現(xiàn)的問題該如何解決的相關(guān)資料,需要的朋友可以參考下2015-10-10HTTP狀態(tài)代碼及其定義解析 Ajax捕捉回調(diào)錯誤參考
當(dāng)用戶試圖通過 HTTP 訪問一臺正在運行 Internet 信息服務(wù) (IIS) 的服務(wù)器上的內(nèi)容時,IIS 返回一個表示該請求的狀態(tài)的數(shù)字代碼。狀態(tài)代碼可以指明具體請求是否已成功,還可以揭示請求失敗的確切原因2013-11-11SpringMVC環(huán)境下實現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)
這篇文章主要介紹了SpringMVC環(huán)境下實現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-05-05使用Ajax技術(shù)通過XMLHttpRequest對象完成首頁登錄功能
這篇文章主要介紹了使用Ajax技術(shù)通過XMLHttpRequest對象完成首頁登錄功能,很不錯的嘗試,需要的朋友可以參考下2014-08-08jQuery ajax中使用serialize()方法提交表單數(shù)據(jù)示例
使用jQuery ajax的serialize() 方法表單序列化為鍵值對(key1=value1&key2=value2…)后提交,下面是使用serialize()序列化表單的實例,感興趣的朋友可以參考下2013-10-10