码迷,mamicode.com
首页 > Web开发 > 详细

ajax 登陆

时间:2016-06-20 06:34:22      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

JS部分
1
//传递登陆的密码到一般处理程序 2 $.post("/index.ashx", { "username": username, "pwd": pwd,"option":option, "cmd": "login" }, function (data) { 3 //data是一般处理程序处理后的返回结果 4 //将Json变成js数组 5 var data = eval(( + data + )); 6 7 if (data.Success) { 8 layer.msg(data.Infor, { 9 icon: data.Ico 10 }, function () { 11 location.href = "../aspx/SerachBooks.aspx"; 12 }); 13 } 14 else { 15 layer.msg(data.Infor, { 16 icon: data.Ico 17 },function () { 18 location.href = "index.html"; 19 }); 20 } 21 });

 

 

 

后台处理

 

 

 

 

 

 

 

 

  1 using com.DAL.Base;
  2 using DAL.library;
  3 using Model.library;
  4 using System;
  5 using System.Collections.Generic;
  6 using System.Linq;
  7 using System.Web;
  8 using System.Web.Script.Serialization;
  9 
 10 namespace library_Update1
 11 {
 12     /// <summary>
 13     /// index 的摘要说明
 14     /// </summary>
 15     public class index : IHttpHandler
 16     {
 17         /// <summary>
 18         /// 前端提交的数据的程序入口
 19         /// </summary>
 20         /// <param name="context"></param>
 21         Model.library.ReturnMessage rm = new Model.library.ReturnMessage();//登陆后的返回数据放在此类里面
 22         string sResult = "";//返回结果-成功或者失败
 23         HttpContext context = null;//上下文对象  
 24         string option = ""; //权限处理的依据
 25         bool boption = true;
 26         public void ProcessRequest(HttpContext context)
 27         {
 28             this.context = context; //HttpContext context是html传递过来的上下文
 29             string cmd = context.Request.Form["cmd"].ToString();
 30             switch (cmd)
 31             {
 32                 case "login":
 33                     sResult = UserLogin();
 34                     break;
 35             }
 36             context.Response.Write(sResult);//向前台传递多个值的问题
 37             
 38         }
 39 
 40         /// <summary>
 41         /// 登陆的处理函数并返回结果
 42         /// </summary>
 43         /// <returns></returns>
 44         public string UserLogin()
 45         {
 46             string username = context.Request.Form["username"].ToString();
 47             string pwd = context.Request.Form["pwd"].ToString();
 48             option = context.Request.Form["option"].ToString();
 49 
 50             bool boption = true;
 51             if (option == "普通用户")
 52             {
 53                 boption = false;
 54             }
 55             else if (option == "管理员")
 56             {
 57                 boption = true;
 58             }
 59             #region cookies
 60             HttpCookie power = new HttpCookie("userrole");
 61             power.Values["option"] = option;
 62             power.Expires.AddDays(1);
 63             context.Response.Cookies.Add(power);
 64             #endregion
 65 
 66 
 67 
 68             List<dbParam> list = new List<dbParam>(){
 69                 new dbParam(){ParamName="@NAME",ParamValue=username,ParamDbType=System.Data.DbType.String},
 70                 new dbParam(){ParamName="@PASSWORD",ParamValue=pwd,ParamDbType=System.Data.DbType.String},
 71                 new dbParam(){ParamName="@ISMANAGER",ParamValue=boption,ParamDbType=System.Data.DbType.Boolean}
 72             };
 73             //与数据库查询用户名和密码
 74             USER user = USERDAL.m_USERDAL.GetModel("NAME=@NAME and PASSWORD=@PASSWORD and ISMANAGER=@ISMANAGER", list);
 75             if (user == null)
 76             {
 77                 rm.Success = false;
 78                 rm.Infor = "登陆失败";
 79                 rm.Ico = 5;
 80             }
 81             else
 82             {
 83                 rm.Success = true;
 84                 rm.Infor = "登陆成功";
 85                 rm.Ico = 6;
 86             }
 87             //转换为json格式
 88             JavaScriptSerializer jss = new JavaScriptSerializer();
 89             return jss.Serialize(rm);
 90         }
 91 
 92         /// <summary>
 93         /// 必须要实现的一个接口IHttpHandler
 94         /// </summary>
 95         public bool IsReusable
 96         {
 97             get
 98             {
 99                 return false;
100             }
101         }
102     }
103 }

 

ajax 登陆

标签:

原文地址:http://www.cnblogs.com/anwser-jungle/p/5599363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!