ASP.NET基于JQUERY的AJAX的验证登录(JSON)

近来想做个JQUERY的AJAX的登录验证,登录不是问题,但是JQUERY的数据验证格式是JSON格式,这下有点难道我了,

因为我以前没学过JSON,于是上百度和google上搜索关于这方面的案例,但是搜到都是PHP的,.NET的少之又少(几乎没有益一个完整的DEMO),由于现在PHP开源框架非常多,而且他们的内部已经封装好了对JSON数据的转化,所以开发人员根本不必关心它内部是怎么转化的,所以对于PHP来说这个JQUERY验证就容易的所了。

 

 

 

下面是自己写的DEMO,

注意看我的编写目录。

 

 

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>主页</title>
   <script src="js/jquery-1.2.6.js" type="text/javascript"></script>
<script src="js/thickbox.js" type="text/javascript"></script>
<link type="text/css"  href="css/thickbox.css" rel="Stylesheet" />
</head>
<body>
<form id="form1" runat="server">
    <div style="margin-left:auto; margin-right:auto;width:400px;">
    <a href="ajaxLogin.html?height=120&width=250&modal=false" class="thickbox" title="请登录">
    我要进行JQUERY登录验证</a>
    <br />
    账号:admin<br/>
    密码:admin<br />
    </div>
   
</form>

</body>
</html>
login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>登录验证</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
    </div>
    </form>
</body>
</html>

login.aspx.cs

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;
public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //执行登录验证的方法
      checklogin();
      
    }
    public void checklogin()
    {
        //获得登录页面POST过来的参数
        string username = Request.Params["id"].ToString();
        string userpwd = Request.Params["pwd"].ToString();
        if (username == "admin" && userpwd == "admin")
        {
            //如果登录成功则构造这样序列化好的JSON格式的数据
           // 这里我用1来表示成功,大家可以随便用什么表示都可以
            Response.Write(CreareJson("这里面可以随便写点什么", 1));
        }
       else
        {
           // 否则是这样的
            Response.Write( CreareJson("这里面可以随便写点什么", 0));
        }
       // end方法一定要写 终止客户端的执行
        Response.End();
    }
    /// <summary>
    /// 定义一个方法用来输出标准的JSON格式数据
    /// </summary>
    /// <param name="info">用来描述一般字符串</param>
    /// <param name="sta">这个用来表示和ajax传输过来数据比较的一个key和value,不一定非用这个表示</param>
    /// <returns>返回标准JSON格式字符串</returns>
    private string CreareJson(string info,int sta) 
    {
        return "{\"info\":\"" + info + "\",\"sta\":" + sta + "}";
    }
}

 

ajaxLogin.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>登录</title>
         <script src="js/login.js" type="text/javascript"></script>
     <style  type="text/css">
      .red{
     color:#ff0000;
     }
     </style>
</head>
<body>
<form method="post" action="login.aspx" id="login_form"  name="login_form">

<div style="text-align:center ">
<table border="0" cellpadding="3" cellspacing="3" style="margin:0 auto;" >
  <tr>
    <td><label> Username</label>
      :</td>
    <td><input name="login_id" id="login_id" type="text" size="20"/></td>
  </tr>
  <tr>
    <td><label> Password</label>
      :</td>
    <td><input name="login_pwd" id="login_pwd" type="password" size="20"/></td>
  </tr>
  <tr align="right">
    <td colspan="2">
    <input type="button" id="LoginBtn" value="login" /> <input type="button" id="Submit1" value="Cancel" οnclick="tb_remove()"/></td>
    </tr>
</table>
<div id="confirm"></div>
</div>
</form>
</body>
</html>

 

login.js

// JScript 文件
$(document).ready(function(){
          //获取登录按的事件并激活click事件
        $('#LoginBtn').click(function(){
             chacklogin();      
        });
    });
    
function chacklogin()
{
  var login_id=$('#login_id').val();
  var login_pwd=$('#login_pwd').val();
  if (login_id=='')
  {
    $('#confirm').html('请输入登录ID');
    $('#login_id').focus();
    return false;
  }
  if(login_pwd =='')
  {
    $('#confirm').html('请输入登录密码');
    $('#login_pwd').focus();
    return false;
  }

 $.ajax({
  type: 'POST',//URL方式为POST
  url: 'login.aspx',//这里是指向登录验证的页面
  data:'id='+login_id+'&pwd='+login_pwd,//把要验证的参数传过去
  dataType:'json',//数据类型为JSON格式的验证
  //在发送数据之前要运行的函数
  beforeSend:function(){
  $('#confirm').html('登录中.........');
  },
  success:function(data)
          {
                //这是个重点,根据验证页面(login.aspx)输出的JSON格式数据判断是否登录成功
                //这里我用1表示的
                //sta就是那个输出到客户端的标示
                if(data.sta==1)
                {
                    $('#confirm').html('登录成功!');location.href='loginOK.htm';
                }
                else
                {
                   $('#confirm').html('密码都没输入正确还想进,哼!').addClass('red');
                
                }
        }
  });
}

 

转载于:https://www.cnblogs.com/freedom831215/archive/2010/02/21/1670673.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值