马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#实现AJAX异步登陆[代码分享]-ASP.NET

[复制链接]
查看5190 | 回复1 | 2013-11-28 18:18:07 | 显示全部楼层 |阅读模式
在ASP.NET中实现AJAX无刷新登陆教程。

IBC会员@璨淼☆星博 提出的教程

首先 : 我们创建一个一般处理程序,命名为:User.ashx,内部代码如下:

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    /// <summary>
    /// User 的摘要说明
    /// </summary>
    public class User : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string name = context.Request.QueryString["name"].ToString();
            string pwd = context.Request.QueryString["pwd"].ToString();
            string mes = "";
            if (name == "admin" && pwd == "123456")
            {
                mes = "true";
            }
            else
            {
                mes = "false";
            }
            context.Response.ContentType = "text/plain";

            context.Response.Write(mes);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}



第二步:Default.aspx页面代码如下:

[C#] 纯文本查看 复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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>
</head>

<script type="text/javascript">
    function login_onclick() {
            var name = document.getElementById("name").value;

            var pass = document.getElementById("pass").value;

            var url = "User.ashx?name="+name+"&pwd="+pass;


            //定义全局xhr
            var xhr;
            if (window.ActivexObject) {
                xhr = new ActivexObjext("Microsoft.XMLHTTP");
            } else if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            }


            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var dom = xhr.responseText;

                    if (dom == "true") {
                        alert('成功');

                    } else {
                        alert('失败');
                    }

                }
            }

            xhr.open("GET", url, true);

            xhr.send(null);
    }

</script>
<body>
   
    <form id="form1" runat="server">
    
    <div>
       
        <asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
        <asp:TextBox ID="name" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
        <asp:TextBox ID="pass" runat="server"></asp:TextBox>
        <br />
        <input type=button value='登陆'/>
 
    
        
    
    </div>
    </form>
</body>
</html>





到这简单的无刷新登陆,就完成了。  

需要注意的代码:

[JavaScript] 纯文本查看 复制代码
<script type="text/javascript">
    function login_onclick() {
            var name = document.getElementById("name").value;

            var pass = document.getElementById("pass").value;

            var url = "User.ashx?name="+name+"&pwd="+pass;


            //定义全局xhr
            var xhr;
            if (window.ActivexObject) {
                xhr = new ActivexObjext("Microsoft.XMLHTTP");
            } else if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            }


            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var dom = xhr.responseText;

                    if (dom == "true") {
                        alert('成功');

                    } else {
                        alert('失败');
                    }

                }
            }

            xhr.open("GET", url, true);

            xhr.send(null);
    }

</script>




这个原理很简单,  就是把用户名跟密码发送给一般处理程序,一般处理程序响应给ajax结果,在script中,得到结果以后进行处理。

要提出的教程访问:
http://www.ibcibc.com/thread-3310-2-1.html


要提出的问题访问:
http://www.ibcibc.com/forum-40-1.html

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
飞/可爱朋 | 2014-5-7 23:42:10 | 显示全部楼层
不错
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则