Jquery and ashx achieve login of ajax

front dc:

<!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="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        //用jquery实现无刷新的登录验证 
        function login() {
            $.ajax({
                url: 'Login.ashx', //访问路径 
                data: 'username=' + $("#username").val() + "&password=" + $("#password").val(), //需要验证的参数 
                type: 'post', //传值的方式 
                error: function () {//访问失败时调用的函数 
                    alert("链接服务器错误!");
                },
                success: function (msg) {//访问成功时调用的函数,这里的msg是Login.ashx返回的值 
                    //alert(msg);
                    $("#a").html(msg);
                }
            });
        }
    </script>
</head>
<body>
    <input type="text" id="username" />
    <input type="text" id="password" />
    <input type="button" value="登录" onclick="login()" />
    <div id="a">
        <!--这里显示返回信息-->
    </div>
</body>
</html>

ashx dc:

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

using System;
using System.Web;

public class login : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string username = context.Request.Form["username"];
        string password = context.Request.Form["password"];
        if (username == "admin" && password == "admin")
        {
            context.Response.Write("登录成功!");
        }
        else
        {
            context.Response.Write("登录失败!用户名和密码错误!");
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

 

posted @ 2013-02-10 22:30  技术笔记2012  阅读(738)  评论(0编辑  收藏  举报