1//Ajax Start 2/**////summary> 3///創(chuàng)建回調(diào)對象,如果存在window.XMLHttpRequest()對象,則返回此對象,如果是IE則搜索Msxml2.XMLHTTP各個版本及Microsoft.XMLHTTP并創(chuàng)建對象返回。 4////summary> 5function Ajax_GetXMLHttpRequest() { 6 if (window.XMLHttpRequest) { 7 return new XMLHttpRequest(); 8 } else { 9 if (window.Ajax_XMLHttpRequestProgID) { return new ActiveXObject(window.Ajax_XMLHttpRequestProgID); } else { var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; for (var i = 0; i progIDs.length; ++i) { var progID = progIDs[i]; try { var x = new ActiveXObject(progID); window.Ajax_XMLHttpRequestProgID = progID; return x; } catch (e) { } } } } return null; } /**////summary> ///Ajax回調(diào)。 ////summary> ///param name="type">調(diào)用服務端函數(shù)所在的類包括命名空間(如:NExplus.Controls.SiteHeader)。/param> ///param name="id">客戶端所對應的標記的ID(如:div id="ID">/div>)。/param> ///param name="method">服務端(方法)函數(shù)名稱(被AjaxMethod標記)。/param> ///param name="args">傳到服務器的字符串。/param> ///param name="clientCallBack">同步或異步回調(diào)。/param> ///param name="debugRequestText">調(diào)試/請求字符串。/param> ///param name="debugResponseText">調(diào)試/輸出字符串。/param> ///param name="debugErrors">調(diào)試的錯誤信息。/param> ///param name="includeControlValuesWithCallBack">是否和控件及其值一起回調(diào)。/param> ///param name="url">Url地址。/param> function Ajax_CallBack(type, id, method, args, clientCallBack, debugRequestText, debugResponseText, debugErrors, includeControlValuesWithCallBack, url) {
if (!url) { url = window.location.href; url = url.replace(/\#.*$/, '');//去除URL中標簽部分,即"#"之后的字符串。 //加入?yún)?shù)Ajax_CallBack并設為true,說明是AJAX回調(diào)。 if (url.indexOf('?') > -1) url += "Ajax_CallBack=true"; else { if (url.substr(url.length - 1, 1) == "/") url += "default.aspx";
url += "?Ajax_CallBack=true"; } }
var x = Ajax_GetXMLHttpRequest();//取得XMLHttpRequest對象。 var result = null; if (!x) { result = { "value":null, "error": "NOXMLHTTP"}; if (debugErrors) { alert("error: " + result.error); } if (clientCallBack) { clientCallBack(result); } return result; }
x.open("POST", url, clientCallBack ? true : false);//以Post方式打開對象,這樣在服務端就可以用Request.Form獲取參數(shù)。 x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); if (clientCallBack) { //如果同步,判斷狀態(tài),輸出錯誤消息。 x.onreadystatechange = function() { var result = null;
if (x.readyState != 4) { return; }
if (debugResponseText) { alert(x.responseText); }
try { var result = eval("(" + x.responseText + ")"); if (debugErrors result.error) { alert("error: " + result.error); } } catch (err) { if (window.confirm('The following error occured while processing an AJAX request: ' + err.message + '\n\nWould you like to see the response?')) { var w = window.open(); w.document.open('text/plain'); w.document.write(x.responseText); w.document.close(); }
result = new Object(); result.error = 'An AJAX error occured. The response is invalid.'; }
clientCallBack(result); } } var encodedData = "Ajax_CallBackType=" + type; if (id) { encodedData += "Ajax_CallBackID=" + id.split("$").join(":"); } encodedData += "Ajax_CallBackMethod=" + method; if (args) { for (var i in args) { encodedData += "Ajax_CallBackArgument" + i + "=" + encodeURIComponent(args[i]); } } //如果加入控件,則加入控件數(shù)據(jù)。 if (includeControlValuesWithCallBack document.forms.length > 0) { var form = document.forms[0]; for (var i = 0; i form.length; ++i) { var element = form.elements[i]; if (element.name) { var elementValue = null; if (element.nodeName == "INPUT") { var inputType = element.getAttribute("TYPE").toUpperCase(); if (inputType == "TEXT" || inputType == "PASSWORD" || inputType == "HIDDEN") { elementValue = element.value; } else if (inputType == "CHECKBOX" || inputType == "RADIO") { if (element.checked) { elementValue = element.value; } } } else if (element.nodeName == "SELECT") { elementValue = element.value; } else if (element.nodeName == "TEXTAREA") { elementValue = element.value; } if (elementValue) { encodedData += "" + element.name + "=" + encodeURIComponent(elementValue); } } } } //如果是調(diào)試,則彈出發(fā)送的數(shù)據(jù)。 if (debugRequestText) { alert(encodedData); } x.send(encodedData);//向服務器發(fā)送數(shù)據(jù)。 if (!clientCallBack) { if (debugResponseText) { alert(x.responseText); } result = eval("(" + x.responseText + ")"); if (debugErrors result.error) { alert("error: " + result.error); } } delete x; return result; }