在Web2.0熱潮中,Ajax是人們談論最多的技術術語之一!其實,AJAX是Asynchronous JavaScript and XML的簡寫,是多種技術的綜合。它使用XHTML和CSS標準化呈現,使用DOM實現動態顯示和交互,使用XML和XSTL進行數據交換與處理,使用XMLHttpRequest對象進行異步數據讀取,使用Javascript綁定和處理全部數據。更重要的是它打破了使用頁面重載的慣例技術組合,能夠說AJAX已成爲Web開發的重要武器!javascript
下面是一個簡單的Ajax實例,其中Request.htm是數據請求和展現頁面,Response.asp是後臺數據處理頁面。程序雖簡單,麻雀雖小五臟俱全,但願能對初學者帶來幫助。html
Request.htm頁面:java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>請求頁面</title> <script type="text/javascript"> var XmlRequest=null; try{ XmlRequest=new XMLHttpRequest(); } catch(a){ try{ XmlRequest=new ActiveXObject("MsXml2.XMLHTTP"); }catch(b){ try{ XmlRequest=new ActiveXObject("Microsoft.XMLHTTP"); }catch(c){} } } function Request(){ var id=document.getElementById("text").value; XmlRequest.open("get","Response.asp?max="+id+"&r="+Math.random(),true); XmlRequest.onreadystatechange=Response; XmlRequest.send(null); } function Response(){ if(XmlRequest.readyState==4 && XmlRequest.status==200){ result.innerHTML=XmlRequest.responseText; } } </script> </head> <body> 最大隨機數:<input type="text" id="text" value="100" maxlength="6" /> <input type="button" value="取得隨機數" /><br /> <span style="float:left;">服務器返回:</span><div id="result" style="color:#FF0000;"></div> </body> </html>
Response.asp頁面:ajax
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <% On Error Resume Next dim max max=clng(Request("max")) if err.number=0 then Randomize() Response.Write(int(rnd()*max+1)) else err.clear() Response.Write("您輸入的好像不是數字吧?") end if %>