侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

前端  /  管理员 发布于 5年前   293

代码如下:

// 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 = '<a href="//article/' + url.value + '">' + url.value + '</a>'    }  }  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;  }  /// <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="//article/../JavaScript/logon.js"></script><script language="javascript" type="text/javascript" src=""></script><link href="//article/../CSS/table_css.css" rel="stylesheet" type="text/css" /><link href="//article/CSS/table_css.css" rel="stylesheet" type="text/css" /><body onload="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>

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


  • 上一条:
    AJAX如何实现无刷新登录功能
    下一条:
    js代码实现微博导航栏
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客