Ajax異步調用框架
編輯:AJAX詳解  
var req;
// retrIEve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as Html file
function AJax(url){
req = false;
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
// branch for IE/Windows ActiveX version
} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
if(req) {
req.onreadystatechange = processReqChange; //Once the state changed,the function "processReqChange" will be excuted.
req.open("GET", url, true);
req.send();
}
}
/*Wait for the response*/
function processReqChange()
{
// only if req shows "complete"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
//Here is your bussiness code
procBusiness();
} else {
alert("獲得數據遇到問題!\n請保證您的網絡連接暢通;否則,請及時與管理員聯系,謝謝!:\n" + req.statusText);
}
}
}
/*
Read the XML file and retrive the data */
function procBusiness(){
var xmlResult = req.responseXML;
var root = XMLResult.documentElement;
//TO DO
}