• ASP.NET AJAX:Ajax验证(ajax+Handler处理)


    1,提交页面

    <%@ Page Language="C#" %>
    
    <!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 type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript">
            function checkUser() {
                var username = $("#username").val();
                var password = $("#password").val();
                if (username == "") {
                    alert("请输入用户名!");
                    return false;
                }
                else if (password == "") {
                    alert("请输入密码!");
                    return false;
                }
                var data = {
                    username: encodeURIComponent(username),
                    userpass:encodeURIComponent(password)
                }
                $.post("Handler.ashx", data, function (msg) {
                    if (msg == "1") {
                       // return true;
                    }
                    else {
                        alert("用户名或密码有误!");
                        return false;
                    }
                });
            }
        </script>
    </head>
    <body>
    <h2>Ajax用户登录</h2>
    
    用户名:<input id="username" name="username" type="text" /><br />&nbsp;码:<input id="password" name="password" type="password" /><br />
    <input onclick="checkUser()" type="button" value="登录" />
    
    </body>
    </html>

    2,处理页面1

    Handler.ashx

    <%@ WebHandler Language="C#" Class="Handler" %>
    
    using System;
    using System.Web;
    
    public class Handler : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/plain";
    
            string username = context.Request["username"];
            string password = context.Request["password"];
    
            string flag="0";
            if (username == "sunshine" && password == "m123")
            {
                flag = "1";
            }
                
            context.Response.Write(flag);
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    
    }

     3,处理页面2

    SignInData.aspx.sc

    using System;
    
    public partial class SignInData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {       
            CheckUser();
            Response.End();
    
        }
        /// <summary>
        /// 验证用户
        /// </summary>
        private void CheckUser()
        {
            string username = Request["username"];
            string password = Request["password"];
    
            if (username == "sunshine" && password == "m123")
            {
                Response.Write("1");
            }
            else
            {
                Response.Write("0");
            }
        }
    }
  • 相关阅读:
    javascript模块化进阶
    javascript模块化基础
    css架构探索
    javascript函数基础概念 (补充完结)
    聊聊圣杯布局
    javascript函数基础概念
    yum提示This system is not registered with RHN.RHN support will be disabled.
    Linux分区和挂载硬盘
    Thunderbird扩展
    yum install nginx
  • 原文地址:https://www.cnblogs.com/ylbtech/p/2878673.html
Copyright © 2020-2023  润新知