If you are new to this topic , this file may help you to get close to AJAX.
ok, here I use asp just to do the first test example of this topic.
We have three files here, listed as:
reg.html
<!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=gb2312" />
<title>TEST AJAX</title>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="reg.js"></script>
</head>
<body>
<div id="msg"></div>
ID: <input type="text" name="regid"><br>
Password: <input type="password" name="regpassword"><br>
<input type="button" id="regsubmit" οnclick="Check()" value="Submit">
</body>
</html>
reg.js
//获取页面指定ID公用函数
function GE(a){return document.getElementById(a);}
//表单检测
function Check(){
if(GE('regid').value==''){GE('msg').innerHTML='ID不能为空';return false}
//检测PASSWORD是否为空
if(GE('regpassword').value==''){GE('msg').innerHTML='password 不能为空';return false}
//检测OK后提交数据
//建立XMLHttpRequest对象
var X=new ActiveXObject("Microsoft.XMLHTTP");
//检测浏览器是否支持XMLHttpRequest
if(X){
//禁止客户端再次提交表单
GE('regsubmit').disabled='true';
//onreadystatechange为XMLHttpRequest的状态改变的事件触发器
X.onreadystatechange=function(){
//readyState 对象状态
//0 = 未初始化
//1 = 读取中
//2 = 已读取
//3 = 交互中
//4 = 完成
if(X.readyState==4){
//交互完成的处理
//status,服务器返回的状态码, 200为成功
if(X.status==200){
//运行服务器返回的脚本
eval(X.responseText)
}
//服务器端程序运行失败,返回错误代码
else{GE('msg').innerHTML=X.statusText}
}
};
//获取服务器端数据
//open("method","URL"[,asyncFlag])
//请求的目标 URL, 方法
//采用POST为提交数据
//采用true为异步传输, false为同步传输
X.open('POST','reg.asp',true);
X.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
var SendData = 'regid='+GE('regid').value+'®password='+GE('regpassword').value
X.send(SendData)
}
//不支持的话返回错误提示
else{
GE('msg').innerHTML='你的浏览器不支持XMLHttpRequest'
}
}
reg.asp
<%
dim regid, regpassword, str
regid=Request.Form("regid")
regpassword=Request.Form("regpassword")
if regid="" or regpassword="" then
str = "You must input id and passqord!"
else
str = "Success,ID is " & regid & " , password is" & regpassword
End if
Response.Write "GE('msg').innerHTML='" & str & "';GE('regsubmit').disabled=false"
Response.End
%>
OVER