1//Ajax Start
2/**////<summary>
3///建立回調對象,如果存在window.XMLHttpRequest()對象,則返回此對象,如果是IE則搜尋Msxml2.XMLHTTP各個版本及Microsoft.XMLHTTP並建立對象返回。
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回調。
///</summary>
///<param name="type">調用服務端函數所在的類包括命名空間(如:NExplus.Controls.SiteHeader)。</param>
///<param name="id">用戶端所對應的標記的ID(如:<div id="ID"></div>)。</param>
///<param name="method">服務端(方法)函數名稱(被AjaxMethod標記)。</param>
///<param name="args">傳到伺服器的字串。</param>
///<param name="clientCallBack">同步或非同步回調。</param>
///<param name="debugRequestText">調試/請求字串。</param>
///<param name="debugResponseText">調試/輸出字串。</param>
///<param name="debugErrors">調試的錯誤資訊。</param>
///<param name="includeControlValuesWithCallBack">是否和控制項及其值一起回調。</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中標籤部分,即"#"之後的字串。
//加入參數Ajax_CallBack並設為true,說明是AJAX回調。
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擷取參數。
x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
if (clientCallBack) {
//如果同步,判斷狀態,輸出錯誤訊息。
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]);
}
}
//如果加入控制項,則加入控制項資料。
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);
}
}
}
}
//如果是調試,則彈出發送的資料。
if (debugRequestText) {
alert(encodedData);
}
x.send(encodedData);//向伺服器發送資料。
if (!clientCallBack) {
if (debugResponseText) {
alert(x.responseText);
}
result = eval("(" + x.responseText + ")");
if (debugErrors && result.error) {
alert("error: " + result.error);
}
}
delete x;
return result;
}