首頁 > web前端 > js教程 > 主體

基於Ajax技術來實現無刷新使用者登入(附程式碼)

php中世界最好的语言
發布: 2018-04-02 16:34:22
原創
1794 人瀏覽過

這次帶給大家基於Ajax技術來實現無刷新用戶登入(附程式碼),基於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 = '<a href="&#39; + url.value + &#39;">' + 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="../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 onload="OnLoad_checkCode();">
<p>
<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 >
        <p id="Url"></p>
      </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>
</p>
</body>
登入後複製

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

AJAX實作不刷新登入

新必學的Ajax總結

以上是基於Ajax技術來實現無刷新使用者登入(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!