小弟才学习Ajax技术,因为是自学的。现在遇到问题,想请教大神,前辈。。做了一个测试:
asp代码:
    <script type="text/javascript" src="jQuery/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#loginBtn").click(function () {
                alert($("#userName").val());
                $.ajax({
                    type: "post",
                    url: "TestAbc.ashx",
                    data: { userName: $("#userName").val(), userPwd: $("#userPwd").val() },
                    success: function (responseText) {
                        if (responseText == "1") {
                            window.location.href = "success.aspx";
                        } else if (responseText == "0") {
                            alert("登录失败...");
                        }
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="userName" runat="server"></asp:TextBox>
        <asp:TextBox ID="userPwd" runat="server"></asp:TextBox>
        <asp:Button ID="loginBtn" runat="server" Text="登 录"></asp:Button>
    </div>
ashx 代码:
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Login ll = new Login();
            string userName=context.Request.Params["userName"].ToString();
            string userPwd=context.Request.Params["userPwd"].ToString();
            UserLogin ul = ll.login(userName,userPwd);
            if (null != ul)
            {
                context.Response.Write("1");
            }
            else
            {
                context.Response.Write("0");
            }
        }        public bool IsReusable
        {
            get
            {
                return false;
            }
为什么不能实现跳转?哪里写错了?求高手指点

解决方案 »

  1.   

    很明显,不能用服务器控件,会导致页面回发修改如下,代码应该没什么问题
    <div>
            <input type="text" id="userName"/>
            <input type="password" id="userPwd"/>
            <input type="button" id="loginBtn" value="登录"/>
        </div>
      

  2.   

    既然用了AJAX,没必要用服务器控件,服务器控件会导致页面刷新,你可以尝试alert(responseText); 弹出返回结果,当时服务器控件的时候,返回值始终为空,不符合条件判断,因此不会跳转.
      

  3.   

    我在success下面alert了,换成服务器控件,不会出来提示信息。为何?
      

  4.   

    如果使用Ajax了,是不是就不能用服务器控件来进行操作?