ajax验证登录

html中的代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="js/jquery-1.7.2.min.js"></script>
</head>
<body>
    用户名:<input type="text" id="text" />
    <br />
    <br />&nbsp;码:<input type="password" id="pwd" />
    <br />
    <br />
    <input type="button" id="btn" value="登录" />
    <br />
    <br />
    <span id="span"></span>

    <script type="text/javascript">
        $("#btn").click(function () {
            var name = $("#text").val();
            var pwd = $("#pwd").val();

            $.ajax({
                url: "ashxs/denglu.ashx",
                data: { "name": name, "pwd": pwd },
                type: "post",
                dataType: "json",
                success: function (data) {
                    if (data.has == "0") {
                        $("#span").text("用户名密码不正确!");
                    }
                    else {
                        window.location.href = "主页.html";
                    }
                },//success
                error: function () {
                    $("#span").text("服务器连接失败");
                },//error
                beforeSend: function () {
                    $("#text").attr('disabled', 'disabled');
                    $("#pwd").attr('disabled', 'disabled');
                    $("#btn").attr('disabled', 'disabled');
                    $("#btn").val('登陆中...');
                },//beforeSend
                complete: function () {
                    $("#text").removeAttr('disabled');
                    $("#pwd").removeAttr('disabled');
                    $("#btn").removeAttr('disabled');
                    $("#btn").val('登  陆');
                }
            });//ajax
        });//btn.click
    </script>
</body>
</html>

一般处理程序中的代码:

<%@ WebHandler Language="C#" Class="denglu" %>

using System;
using System.Web;
using System.Linq;
using System.Data.Linq;
using System.Collections;
using System.Collections.Generic;

public class denglu : IHttpHandler
{
    DataClasses2DataContext con = new DataClasses2DataContext();
    public void ProcessRequest(HttpContext context)
    {
        System.Threading.Thread.Sleep(3000);
        string name = context.Request["name"];
        string pwd = context.Request["pwd"];
        string end = "0";

        var data = con.zhuce.Where(r => r.name == name && r.pwd == pwd).ToList();

        if (data.Count() > 0)
        {
            end = "1";
        }
        context.Response.Write("{"has":"" + end + ""}");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
原文地址:https://www.cnblogs.com/123lucy/p/5770006.html