簡單ajax實(shí)例,最具兼容性,簡單介紹ajax運(yùn)行模式,根據(jù)此自認(rèn)為最好的模式可以設(shè)計(jì)出很好的應(yīng)用。
asycn.txt文檔
Hello client!
async.html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>AJAX Foundations: Using XMLHttpRequest</title>
<script type="text/javascript" src="async.js"></script>
</head>
<body onload="process()">
Hello, server!
<br/>
<div id="myDivElement" />
</body>
</html>
async.js文件
// holds an instance of XMLHttpRequest
//建立一個(gè)XmlHttpRequestObject對象實(shí)例
var xmlHttp = createXmlHttpRequestObject();
// creates an XMLHttpRequest instance
//建立XmlHttpRequestObject對象
function createXmlHttpRequestObject()
{
// will store the reference to the XMLHttpRequest object
//用于存儲(chǔ)XmlHttpRequest對象的引用
var xmlHttp;
// this should work for all browsers except IE6 and older
//創(chuàng)建除了ie6 或者其更早版本外的所有瀏覽器
//(用try catch結(jié)構(gòu)是我見過最好的最具兼容性的創(chuàng)建XMLHttpRequest對象實(shí)例的方法)
try
{
// try to create XMLHttpRequest object
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// assume IE6 or older
//假設(shè)是ie6 或其更早版本
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
// try every prog id until one works
//順序嘗試創(chuàng)建每一個(gè)對象,直到成功為止
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
{
try
{
// try to create XMLHttpRequest object
//嘗試創(chuàng)建XMLHttpRequest對象
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e) {}
}
}
// return the created object or display an error message
//返回已經(jīng)創(chuàng)建的對象,或顯示錯(cuò)誤信息
//實(shí)際應(yīng)用中這里最好不要把錯(cuò)誤信息發(fā)送到客戶端
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
// called to read a file from the server
// 創(chuàng)建process()函數(shù),讀取服務(wù)器上的回顯文本
function process()
{
// only continue if xmlHttp isn't void
// 當(dāng) xmlHttp不為空時(shí)繼續(xù)
if (xmlHttp)
{
// try to connect to the server
//嘗試連接服務(wù)器
try
{
// initiate reading the async.txt file from the server
//開始讀取服務(wù)器上的async.txt文件,也可以是
http://www.a.com/index.php?app=ajax&act=response xmlHttp.open("GET", "async.txt", true); //在這里只是設(shè)置,發(fā)送。設(shè)置get方法提交的request參數(shù),像是在地址欄中輸入
http://async.txt,調(diào)用方法用異步(或者解釋成設(shè)置異步讀取的文件async.txt)
xmlHttp.onreadystatechange = handleRequestStateChange; //設(shè)置XMLHttpRequest處理狀態(tài)變化的函數(shù)。
xmlHttp.send(null); //把以上設(shè)置發(fā)送到服務(wù)器,get方法直接在以上的open方法設(shè)置提交參數(shù),如果為post,設(shè)置send(app=ajax&act=response)
}
// display the error in case of failure
//如果出現(xiàn)異常,顯示錯(cuò)誤信息
catch (e)
{
alert("Can't connect to server:\n" + e.toString());
}
}
}
// function that handles the HTTP response
//處理http響應(yīng)的函數(shù)
function handleRequestStateChange()
{
// obtain a reference to the <div> element on the page
//獲取頁面上<div>元素的id
myDiv = document.getElementById("myDivElement");
// display the status of the request
//依次顯示請求狀態(tài)信息
if (xmlHttp.readyState == 1)
{
myDiv.innerHTML += "Request status: 1 (loading) <br/>";
}
else if (xmlHttp.readyState == 2)
{
myDiv.innerHTML += "Request status: 2 (loaded) <br/>";
}
else if (xmlHttp.readyState == 3)
{
myDiv.innerHTML += "Request status: 3 (interactive) <br/>";
}
// when readyState is 4, we also read the server response
//當(dāng)轉(zhuǎn)換到狀態(tài)4時(shí),讀取服務(wù)器的響應(yīng)
else if (xmlHttp.readyState == 4)
{
// continue only if HTTP status is "OK"
// xmlHttp.status為200時(shí)表示處理成功
if (xmlHttp.status == 200)
{
try
{
// read the message from the server
//讀取服務(wù)器信息
response = xmlHttp.responseText;
// display the message
//顯示信息到指定id
myDiv.innerHTML +=
"Request status: 4 (complete). Server said said: <br/>";
myDiv.innerHTML += response;
}
catch(e)
{
// display error message
alert("Error reading the response: " + e.toString());
}
}
else
{
// display status message
alert("There was a problem retrieving the data:\n" +
xmlHttp.statusText);
}
}
}