$.ajax()验证登录

小提醒,在jquery的ajax函数中,可以传入3种类型的数据

1.文本:"uname=alice&mobileIpt=110&birthday=1983-05-12"

2.json对象:{uanme:'vic',mobileIpt:'110',birthday:'2013-11-11'}

3.json数组:

[
    {"name":"uname","value":"alice"},
    {"name":"mobileIpt","value":"110"},   
    {"name":"birthday","value":"2012-11-11"}
]
$.ajax()验证登录
<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; } } }

 

posted @ 2017-03-26 20:49  iriliguo  阅读(438)  评论(0编辑  收藏  举报