基于Ajax技术实现无刷新用户登录功能 - 网站

基于Ajax技术实现无刷新用户登录功能

分类:js_jQuery答疑 - 常见问题 · 发布时间:2021-11-16 17:14 · 阅读:5465

这篇文章主要介绍了基于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 = ""; OnLoad_checkCode(); } } function CallBack(res) { if(res.value == true) { hideLogon(); var url = userControl_logon.returnUrl(); if ( url.value == 404) { showDefault(); } else { document.getElementById("Url").innerHTML = '' + url.value + '' } } else { alert("对不起你的权限不够"); document.getElementById("txtpassword").value = ""; OnLoad_checkCode(); } } //隐藏登录框 function hideLogon() { var element = document.getElementById("hideLogon") element.style.display = "none" } //显示返回首页 function showDefault() { var element = document.getElementById("Returndefault") element.style.display = "block" } function OnLoad_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; } ///  /// 判断验证是否正确 ///  ///  ///  [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; } } ///  /// 判断用户名及密码添加是否正确 ///  /// 用户名 /// 用户名密码 /// bool [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; } } ///  /// 判断用户是不是有这进入管理留言版的权限 ///  /// 用户名 ///  [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; } } ///  /// 返回Url值 ///  ///  [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" %> 
用户名:
密 码:
验证码:

以上所述是小编给大家介绍的基于Ajax技术实现无刷新用户登录功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对html中文网网站的支持!

标签:
ajax 无刷新 登录

相关文章

AJAX乱码与异步同步以及封装jQuery库实现步骤详解

这篇文章主要介绍了异步通信技术AJAX乱码问题、异步与同步、手动封装一个jQuery库,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

AJAX的跨域问题解决方案

跨域简单的说,就是从一个域名的网页去访问另一个域名网页的资源,下面这篇文章主要给大家介绍了关于AJAX的跨域问题解决的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

利用Ajax实现智能回答的机器人示例代码

这篇文章主要介绍了利用Ajax实现智能回答的机器人,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

ajax、axios和fetch之间优缺点重点对比总结

今天被问到用没用过ajax axios,我回答经常用axios,但ajax用的比较少,下面这篇文章主要给大家介绍了关于ajax、axios和fetch之间优缺点重点对比总结的相关资料,需要的朋友可以参考下

如何通过axios发起Ajax请求(最新推荐)

Axios是专注于网络数据请求的库,相比于原生的XMLHttpRequest对象,axios简单易用。相比于Jquery,axios更加轻量化,只专注于网络数据请求,这篇文章主要介绍了如何通过axios发起Ajax请求,需要的朋友可以参考下

返回分类 返回首页