jquery ajax 学习3

 最近在学习ajax,先把两个最常用的jqeury ajax方法记下来:

 $.get 写一个最容易的登陆的方法:

 前台页面代码如下:

<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {

            $("#tijiao").click(function () {
                     
var $txtname = $("#txtname").val(); var $txtpassword = $("#txtpassword").val(); $.get("Handler2.ashx", { names: $txtname, pawdes: $txtpassword }, function (date) { if (date == 1) { alert("成功") window.location.href = "Default7.aspx"; } else { alert("失败") } }) }) }) </script> </head> <body> <form id="form1" runat="server"> <div> <input id="txtname" type="text" name="name" value="" /><br /> <input id="txtpassword" type="text" name="names" value="" /><br /> <input id="tijiao" type="button" value="提交" /> </div> </form> </body> </html>

 2,在一个ashx里面写登陆方法,如果用户名和密码都正确,反回1,反之反回0,然后在前台页面去判断。 在这里就不再去BLL层连数据库了,主要是学习Jquery ajax

public class Handler2 : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string names = context.Request.QueryString["names"];//传过来的用户名
        string pawdes = context.Request.QueryString["pawdes"];//传过来的密码
        int t = 0;
        if (names == "admin" && pawdes == "admin")
        {
            t = 1;
            context.Response.Write(t);
        }
        else
        {
            t = 0;
           context.Response.Write(t);
        }
        
    
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

  再写一个$.post方法登陆

<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {

            $("#tijiao").click(function () {
                var $txtname = $("#txtname").val();

                var $txtpawd = $("#txtpawd").val();

                $.post("Handler.ashx", { names: $txtname, pawdes: $txtpawd }, function (date) {
                  
                    if (date == 1) {
                        alert("成功")
                        window.location.href = "Default7.aspx";
                    } else {
                        alert("失败")
                    }

                })


            })





        })
    </script>
</head>
<body>
    <form id="form1" runat="server" action="Handler.ashx" method="post"  >
   <div>
       <input id="txtname" type="text" name="name" value="" /><br />
        <input id="txtpawd" type="text" name="names" value="" /><br />
        <input id="tijiao" type="button" value="提交" />
   </div>
    
   
   
    </form>
</body>
</html>

  然后也是在ashx里面判断

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        
        string names = context.Request.Form ["names"];
        string pawdes = context.Request.Form["pawdes"];
        int t = 0;
        if (names == "admin" && pawdes == "admin")
        {
            t = 1;
            context.Response.Write(t);
        }
        else
        {
            t = 0;
           context.Response.Write(t);
        }
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

  好的,今天先到这里

原文地址:https://www.cnblogs.com/xu3593/p/2825206.html