简体   繁体   中英

Redirect to another page does not work in ajax call

I have written one ajax post request on aspx page which will call web method written in its code behind class.This method return url to redirect..All is working fine till success function of ajax call but in success function I'm redirecting to another page ex.

   window.location.assign(data.d)

I have checked data.d result via alert in success function which is showing correct url but its not rediecting to that page..Plz help..

Full code is here..

This is script:

 <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>

and following is the web method:

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

        }

Please note: If I uncomment alert("dsf"),all works fine it redirects successfully to Home.aspx..But without this alert it wont redirect.

尝试这个

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

Try this

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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