<script type="text/javascript" language="javascript"> function IbtnEnter_onclick() { checklogin(); return false; } function checklogin() { if ($("#TxtUserName").val() == "") { alert("用户名不能为空!"); $("#TxtUserName").focus(); return false; } if ($("#TxtPassword").val() == "") { alert("密码不能为空!"); $("#TxtPassword").focus(); return false; } $.ajax({ type: "POST", url: "ajax/Handler.ashx?M=" + Math.random(), data: "username=" + $("#TxtUserName").val().toString() + "&pwd=" + $("#TxtPassword").val().toString(), success: function (data) { if (data == "1") { location.href = "index.aspx"; return true; } else { alert("请确认您输入的用户名或密码输入是否正确!"); $("#TxtUserName").val(""); $("#TxtPassword").val(""); $("#TxtUserName").focus(); return false; } } }) } </script>
一般处理程序
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; using System.Data.SqlClient; using System.Web.SessionState;//继承接口IReadOnlySessionState需要引入的命名空间 public class Handler : IHttpHandler, IRequiresSessionState { SqlHelper helper = new SqlHelper(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string username = context.Request.Params["username"].ToString().Trim(); string pwd = context.Request.Params["pwd"].ToString().Trim(); if (username != "" && pwd != "") { string sql = @"SELECT * FROM [USER] WHERE USERNAME='"+username+"' AND PASSWORD='"+pwd+"' "; if (!helper.Exists(sql)) { context.Response.Write("0"); } else { SqlDataReader reader = helper.ExecuteReader(sql); while (reader.Read()) { context.Response.Write("1"); context.Session["username"] = username.ToString().Trim(); context.Session["pwd"] = pwd.ToString().Trim(); } } } } public bool IsReusable { get { return false; } } }
作者:
望月狼
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.http://www.cnblogs.com/wangyuelang0526/archive/2012/06/01/2530790.html