基于Ajax技术实现无刷新用户登录功能
代码如下:
// JScript 文件 function usersLogon() { var userName = document.getElementById("txtuserName").value; var password = document.getElementById("txtpassword").value; var checkCode = document.getElementById("txtCheckCode").value; var response = userControl_logon.CheckCodeIsRight(checkCode).value; if(userName == "") { document.getElementById("txtuserName").focus(); return false; } else if(password == "") { document.getElementById("txtpassword").focus(); return false; } else if(checkCode =="") { document.getElementById("txtCheckCode").focus(); return false; } else { if(response == true) { //判断用户是否存在 userControl_logon.userNameAndPasswordIsExist(userName,password,userNameIsRight); } else { alert("验证码出错"); userControl_logon.checkCodeOperaotr(refreshCheckCode); document.getElementById("txtpassword").value = ""; } } } function userNameIsRight(res) { var userName = document.getElementById("txtuserName").value; if(res.value == true) { //用户存在,但要看此用户有没有进入管理留言版权限, userControl_logon.userNameIsRight(userName,CallBack); } else { alert("用户名或密码错误"); document.getElementById("txtpassword").value = ""; onl oad_checkCode(); } } function CallBack(res) { if(res.value == true) { hideLogon(); var url = userControl_logon.returnUrl(); if ( url.value == 404) { showDefault(); } else { document.getElementById("Url").innerHTML = '<a href="' + url.value + '">' + url.value + '</a>' } } else { alert("对不起你的权限不够"); document.getElementById("txtpassword").value = ""; onl oad_checkCode(); } } //隐藏登录框 function hideLogon() { var element = document.getElementById("hideLogon") element.style.display = "none" } //显示返回首页 function showDefault() { var element = document.getElementById("Returndefault") element.style.display = "block" } function onl oad_checkCode() { userControl_logon.checkCodeOperaotr(refreshCheckCode); document.getElementById("txtuserName").focus(); // return false; } ///重新得到新的验证吗 function refreshCheckCode(res) { document.getElementById("txtCheckCode").value = ""; document.getElementById("lblNumber").innerHTML = res.value; } function abce() { alert(document.getElementById("lblNumber").value) }
下面代码
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using LHB_SQL_2005; public partial class userControl_logon : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { AjaxPro.Utility.RegisterTypeForAjax(typeof(userControl_logon)); } } [AjaxPro.AjaxMethod] public static string checkCodeOperaotr() { string _checkCode = GeneralMethod.GenerateCheckCode(); System.Web.HttpContext.Current.Session["checkCode"] = _checkCode; //返回验证码 return _checkCode; } /// <summary> /// 判断验证是否正确 /// </summary> /// <param name="checkCode"></param> /// <returns></returns> [AjaxPro.AjaxMethod] public static bool CheckCodeIsRight(string checkCode) { string _checkCode = (string)(System.Web.HttpContext.Current.Session["checkCode"]); if (_checkCode == checkCode) { return true; } else { return false; } } /// <summary> /// 判断用户名及密码添加是否正确 /// </summary> /// <param name="userName">用户名</param> /// <param name="_password">用户名密码</param> /// <returns>bool</returns> [AjaxPro.AjaxMethod] public static bool userNameAndPasswordIsExist(string userName, string _password) { string password = GeneralMethod.ToEncryptPassword(_password); string executeString = "SELECT COUNT(*) FROM users WHERE userName = '" + userName.ToString() + "' AND password = '" + password + "'"; int count = int.Parse(GetCommand.ExecuteScalar(executeString)); if (count == 1) { System.Web.HttpContext.Current.Session["userName"] = userName; return true; } else { return false; } } /// <summary> /// 判断用户是不是有这进入管理留言版的权限 /// </summary> /// <param name="userName">用户名</param> /// <returns></returns> [AjaxPro.AjaxMethod] public static bool userNameIsRight(string userName) { string executeString = "SELECT [right] FROM role WHERE usersId = (select userNameId from users where userName = '" + userName + "')"; int count = int.Parse(GetCommand.ExecuteScalar(executeString)); if (count > 0) { return true; } else { return false; } } /// <summary> /// 返回Url值 /// </summary> /// <returns></returns> [AjaxPro.AjaxMethod] public static string returnUrl() { string url = ""; try { url = System.Web.HttpContext.Current.Session["url"].ToString(); } catch { url ="404"; } return url; } }
下面是页面代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="logon.ascx.cs" Inherits="userControl_logon" %> <script language="javascript" type="text/javascript" src="../JavaScript/logon.js"> </script> <script language="javascript" type="text/javascript" src="JavaScript/logon.js"> </script> <link href="../CSS/table_css.css" rel="stylesheet" type="text/css" /> <link href="CSS/table_css.css" rel="stylesheet" type="text/css" /> <body onl oad="OnLoad_checkCode();"> <div> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table id="hideLogon" border="0" cellpadding="0" cellspacing="0" style="display:block;"> <tr> <td style="background-color: #99ccff">用户名:</td> <td><input type="text" id="txtuserName" style="width: 105px" /></td> </tr> <tr> <td style="background-color: #99ccff">密 码:</td> <td> <input id="txtpassword" type="password" style="width: 105px" /></td> </tr> <tr> <td style="background-color: #99ccff">验证码:</td> <td style="background-color: #99ccff"> <input type= "text" id="txtCheckCode" style=" width:60px" /><label id="lblNumber"></label></td> </tr> <tr> <td style="background-color: #99ccff"></td> <td style="background-color: #99ccff"> <input type="button" onclick="usersLogon();" value="登录" id="btnLogon" /></td> </tr> </table> </td> </tr> <tr> <td > <div id="Url"></div> </td> </tr> <tr> <td align="center"> <table id="Returndefault" border="0" cellpadding="0" cellspacing="0" style="display:none;"> <tr> <td> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">返回首页</asp:HyperLink></td> </tr> </table> </td> </tr> </table> </div> </body>
原文链接:http://blog.csdn.net/hanchuang213/article/details/54314313
(资源库 www.zyku.net)
上一篇:用ajax传递json到前台中文出现问号乱码问题的解决办法
栏 目:AJAX
下一篇:AJAX用于判定用户是否注册
本文标题:基于Ajax技术实现无刷新用户登录功能
本文地址:https://www.zyku.net/ajax/567.html
您可能感兴趣的文章
- 03-31利用ajax+php实现商品价格计算
- 03-30thinkphp5 使用H-ui+ajax 提交数据
- 07-27PHP自定义函数判断是否为Get、Post及Ajax提交的方法
- 07-18使用Ajax方法实现Form表单的提交及注意事项
- 07-18Ajax的特性及乱码问题
- 07-12jQuery+Ajax请求本地数据加载商品列表页并跳转详情页
- 06-26Ajax提交表单并接收json实例代码
- 04-05Ajax验证用户名或昵称是否已被注册
- 04-04php实现用户登录简单实例
- 03-15thinkPHP+ajax实现统计页面pv浏览量的方法
- 09-24oppo手机热点功能在哪里
- 02-26小米手机启用勿扰模式方法
- 12-18HTML <small> 标签
- 01-14无为学校-无为学校应用软件功能介绍
- 01-14智慧眉山-智慧眉山应用软件功能介绍
- 07-18小米手机设置轻敲背部方法分享
- 02-27帝国CMS后台编辑器在IE10下的兼容问题
- 02-23DedeCMS文章内容页显示当前文章最新评
- 02-11pytorch 如何把图像数据集进行划分成t
- 06-24帝国CMS循环子栏目标签listsonclass介
最近更新
阅读排行
猜你喜欢
- 02-22DedeCMS的tag标签不支持大写字母的解
- 04-10黑鲨4防闪烁模式开启教程
- 05-07opporeno5pro流量消耗查看方法
- 01-13织梦DedeCMS缩略图失真模糊完美解决方
- 10-03oppok9pro在哪里查看网速
- 11-19钉钉怎么设置视频美颜
- 06-26highlightjs网页代码高亮插件调用方法
- 08-27FileZilla传输代码文件时被换行的解决
- 04-18一加9pro录制视频关闭屏幕点击动作教
- 01-08iQOO9语音唤醒功能在哪