测试通过环境VS2008,添加2个新项
1:DEMO3.ASPX
复制代码 代码如下:
//查询字符串,SEND服务端的参数
function createQueryString() {
var userName = document.getElementById(“userName”).value;
var querystring = “userName=” + userName;
return querystring;
}
function RunAjax() {
//参考’摘自书中有意义的示例中DEMO1.HTML如何创建XMLHttpRequest对象 代码’
createXMLHttpRequest();
var queryString = “IsHaveUser.ashx?”;
queryString = queryString + createQueryString()
+ “&timeStamp=” + new Date().getTime();//URL上追加时间戳,防止浏览器缓存
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open(“GET”, queryString, true);
xmlHttp.send(null);
document.getElementById(‘validateMessage’).innerText = ‘正在检查,请稍候…’;
}
function handleStateChange() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var result = xmlHttp.responseText;
document.getElementById(“validateMessage”).innerText = result;
if (result== “恭喜!可以注册…”) {
setTimeout(“document.getElementById(‘validateMessage’).innerText=”;”, 2000);
}
}
}
}
<input /><input type=”button” value=”检查用户是否被注册”/><span ></span>
2:IsHaveUser处理程序
复制代码 代码如下:
public class IsHaveUser : IHttpHandler {
public void ProcessRequest (HttpContext context) {
System.Threading.Thread.Sleep(2000);
string userName = context.Request.QueryString[“userName”];
bool result = 读取数据库执行判断用户名是否存在操作,返回布尔值,省略代码
if (result)
{
context.Response.Write(“恭喜!可以注册…”);
}
else { context.Response.Write(“此用户存在,请更换…”); }
}
public bool IsReusable { get { return false;}}
}