測試通過環境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 id="userName" /><input id="IsHaveUser" type="button" value="檢查用戶是否被注冊" onclick="RunAjax();"/><span id="validateMessage" ></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;}}
}