Jquery之Ajax实例_登录

来源:互联网 发布:画派 知乎 编辑:程序博客网 时间:2024/06/06 14:07

一、Login.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.1.js"></script>    <script type="text/javascript">        $(function () {            $("#msg").css("display","none");            $("#btnLogin").click(function () {                userLogin();            });        });        function userLogin() {            var userName = $("#txtUserName").val();            var userPwd = $("#txtUserPwd").val();            if (userName != "" && userPwd != "") {                $.post("UserLogin.ashx", { "userName": userName, "userPwd": userPwd }, function (data) {                    var serverData = data.split(':');                    if (serverData[0] == "ok") {                        window.location.href = "/2015-5-31/UserInfoList.aspx";                    } else {                        $("#msg").css("display", "block");                        $("#msg").text(serverData[1]);                        $("#txtUserName").val("");                    }                });            } else {                $("#msg").css("display", "block");                $("#msg").text("用户名密码不能为空!!");            }        }    </script></head><body>    用户名:<input type="text" name="txtName" id="txtUserName" />    密码:<input type="password" name="txtPWD"  id="txtUserPwd"/><br />        <input type="button" value="登录" id="btnLogin" />    <span id="msg" style="font-size:14px;color:red"></span><br /></body></html>
二、UserLogin.ashx.cs代码
using CZBK.ItcastProject.Model;using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace CZBK.ItcastProject.WebApp._2015_6_2{    /// <summary>    /// UserLogin 的摘要说明    /// </summary>    public class UserLogin : IHttpHandler,System.Web.SessionState.IRequiresSessionState    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            string userName = context.Request["userName"];            string userPwd=context.Request["userPwd"];            BLL.UserInfoService UserInfoService = new BLL.UserInfoService();            string msg = string.Empty;            UserInfo userInfo = null;            if (UserInfoService.ValidateUserInfo(userName, userPwd, out msg, out userInfo))            {                context.Session["userInfo"] = userInfo;                context.Response.Write("ok:"+msg);            }            else            {                context.Response.Write("no:" + msg);            }        }        public bool IsReusable        {            get            {                return false;            }        }    }}


原创粉丝点击