在Web2.0热潮中,Ajax是人们谈论最多的技术术语之一!其实,AJAX是Asynchronous JavaScript and XML的简写,是多种技术的综合。它使用XHTML和CSS标准化呈现,使用DOM实现动态显示和交互,使用XML和XSTL进行数据交换与处理,使用XMLHttpRequest对象进行异步数据读取,使用Javascript绑定和处理所有数据。更重要的是它打破了使用页面重载的惯例技术组合,可以说AJAX已成为Web开发的重要武器!

下面是一个简单的Ajax实例,其中Request.htm是数据请求和展示页面,Response.asp是后台数据处理页面。程序虽简单,麻雀虽小五脏俱全,希望能对初学者带来帮助。


Request.htm页面:

<!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="取得随机数" onclick="Request()" /><br />  
<span style="float:left;">服务器返回:</span><div id="result" style="color:#FF0000;"></div>  
</body>  
</html>  



Response.asp页面:

<%@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  
%>