AJAXユーザー登録検証を実現


htmlページ:




    
    <script src="../Js/jquery-1.7.1.js"/>
    <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 = "/UserInfoList.aspx";
                    } else {
                        $("#msg").css("display", "block");
                        $("#msg").text(serverData[1]);
                        $("#txtUserName").val("");
                    }

                });

            } else {
                $("#msg").css("display", "block");
                $("#msg").text("         !!");
            }
        }
    </script>



       :<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"/><br/>

</code></pre> 
  <span style="font-size:18px;"><strong>UserLogin.ashx.cs   :</strong></span> 
  <br/> 
  <pre><code>using XXX.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace XXX.WebApp
{
    /// <summary>
    /// UserLogin      
    /// </summary>
    public class UserLogin : IHttpHandler,System.Web.SessionState.IRequiresSessionState  // Session      
    {

        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;
            }
        }
    }
}</code></pre> 
  <br/> 
  <br/> 
   
 </div> 
</div>
                            </div>
                        </div>