繁体   English   中英

重定向到另一个页面在ajax调用中不起作用

[英]Redirect to another page does not work in ajax call

我在aspx页面上写了一个ajax发布请求,它将在其类后面调用以其代码编写的web方法。该方法返回URL以进行重定向..在ajax调用成功之前,所有功能都很好,但是在成功功能中,我正在重定向到另一个页前

   window.location.assign(data.d)

我已经通过成功功能中的警报检查了data.d结果,该结果显示了正确的url,但未重新指向该页面。.plz帮助。

完整代码在这里。

这是脚本:

 <script type="text/javascript">
        jQuery(document).ready(function() {
            $('#loginbtn').click(function() {
                var userName = document.getElementById('uid').value;
                var password = document.getElementById('pwd').value;
                $.ajax({
                    type: "POST",
                    url: "testAjax.aspx/Authenticate",
                    data: JSON.stringify({ userName: userName, password: password }),
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) { window.location.assign(data.d); },
                    error: function(e) {
                        alert(e.valueOf());
                    }
                });
                //alert("dsf");
            });

        });
    </script>

以下是Web方法:

 [WebMethod]
        public static string Authenticate(string userName, string password)
        {
            try
            {
                return "Home.aspx";
            }
            catch (Exception ex)
            {
                return string.Empty;
            }

        }

请注意:如果我取消注释alert(“ dsf”),则一切正常,它将成功重定向到Home.aspx。。但是,如果没有此警报,它将无法重定向。

尝试这个

success: function(data) {  window.location=data.ToString(); }

尝试这个

<script type="text/javascript">
    jQuery(document).ready(function() {
        $('#loginbtn').click(function() {
            var userName = document.getElementById('uid').value;
            var password = document.getElementById('pwd').value;
            $.ajax({
                type: "POST",
                url: "testAjax.aspx/Authenticate",
                data: JSON.stringify({ userName: userName, password: password }),
                async: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) { window.location=data.d; },
                error: function(e) {
                    alert(e.valueOf());
                }
            });
            //alert("dsf");
        });

    });
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM