using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace AJAX { /// <summary> /// LoginHandler 的摘要说明 /// </summary> public class LoginHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string UserName = context.Request["UserName"]; string UserPwd = context.Request["UserPwd"]; if (string.IsNullOrEmpty(UserName)) { context.Response.Redirect("Login.html"); } string sql = "select count(*) from Users where UserName=@u "; SqlParameter[] p = { new SqlParameter("@u",UserName)}; int i = Convert.ToInt32(SQLHelper.ExecuteScalar(sql,p)); if (i==1) { context.Response.Write("1"); } else { string s = "insert into Users (UserName,Pwd) values (@UserName,@p)"; SqlParameter[] sp = { new SqlParameter("@UserName",UserName), new SqlParameter("@p",UserPwd)}; int j = Convert.ToInt32(SQLHelper.NonExQuery(s, sp)); if (j == 1) { context.Response.Write("0"); } } } public bool IsReusable { get { return false; } } } }
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <script src="js/jquery-3.3.1.js"></script> <script> $(function () { $("#UserName").blur(function () { var UserName = $("#UserName").val(); $.post("LoginHandler.ashx", { "UserName": UserName},function (data) { if (data == "1") { $('#btn').attr('disabled',"true"); alert("已经注册过了"); } else { $('#btn').removeAttr("disabled"); } }); }); $("#btn").click(function () { var UserName = $("#UserName").val(); var UserPwd = $("#UserPwd").val(); $.post("LoginHandler.ashx", {"UserName": UserName, "UserPwd": UserPwd }, function (data) { if (data == "0") { alert("注册成功!"); } }) }) }); </script> </head> <body> 用户名:<input type="text" name="UserName" id="UserName" /> <br /> 密 码:<input type="password" name="UserPwd" id="UserPwd" /> <br /> <input type="button" value="注册" id="btn" /> </body> </html>