Redirection in Javacsript

3161
3161 used Ask the Experts™
on
I am doing an ajax call to a handler in C# and the handler is returning a successful response. I am try to direct the user to the proper url based on the response So the return value looks like this:

{'Auth':'true', 'URL':'Default.aspx'}

And the call looks like this:

   
$('#cmdSubmit').on('click', function () {


            var _username = $.trim($('#txtUsername').val());
            var _password = $.trim($('#txtPassword').val());
            var _formData = { 'func': 'Auth', 'Username': _username, 'Password': _password };

            $.ajax({
                url: _profileHdlr,
                async: false,
                type: 'GET',
                data: _formData,
                'dataType': 'json',
                success: function (data) {
                    alert(data.Url);     // Th alert fires just fine
                    window.location.href = data.Url;   // The window.location does nothing
                },
                error: function (jqXHR, error, errorThrown) {
                    if (jqXHR.status && jqXHR.status == 400) {
                        alert(jqXHR.responseText);
                    }
                }
            });
        }

Open in new window


I realize this seems simple, I think so also, but the redirection does not work. I already tried window,location.assign and .replace but nothing happens. If anyone has any ideas I would be grateful.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try out this method too
Please paste alert value.

1 > window.navigate("http://lovetomarry.com");
2> self.location="http://lovetomarry.com";
3> top.location="http://lovetomarry.com";

Author

Commented:
I appreciate the quick reply, and believe me I have done this before, so I am very confused about why it is behaving this way. I tried your suggestions and it simply does nothing. I realize I am not giving you anything to go on, but I don't have anything.

The alerts simply posts the page which I want to redirect to, in this case Default.aspx.

Thanks again
i hope value is http://desired url/default.aspx

var url = data.Url;
window.location.href = url;
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Once again thank you for your reply.  To remove any possibility to formatting problems I modified the code to read:

            $.ajax({
                url: _profileHdlr,
                async: false,
                type: 'GET',
                data: _formData,
                'dataType': 'json',
                success: function (data) {                    
                    window.location.href = "http://lovetomarry.com";
                    //alert(data.Url);
                },
                error: function (jqXHR, error, errorThrown) {
                    if (jqXHR.status && jqXHR.status == 400) {
                        alert(jqXHR.responseText);
                    }
                }
            });


Unfortunately I still receive nothing, I even uploaded the code to eliminate the possibility of the localhost being the problem (although it never has been before)

Thank you again
Confused
Even now i am confused . does the result remain same in all browsers or some specific.

try
window.location = "http://lovetomarry.com";
return false;

Or
window.location.href = "http://lovetomarry.com";
return false;

If its in IE only  then add below line

window.event.returnValue=false;

Author

Commented:
I am currently working with Chrome, but I just tried in IE and FireFox and I get the same results. The start page is a Login.aspx, which calls a .js file and contains the code I included with this message.

The .js file makes an ajax call to a handler which authenticates the user and returns the values listed. All this works with no issues. The alert fires but the redirect on the window just sits there.

Not sure if any of that helps, but I thought I would try to detail the process to help in figuring out what the problem is.

I will keep working on it and let you now if I find anything.

Thanks again

Author

Commented:
I am not sure if this will help determine the problem, but in trying to troubleshoot it in Firefox, if I step through it using Firebug, it redirects, if I remove the break point, nothing happens . . . weird
Did u added return false; after redirection code.

Author

Commented:
Thank you once again for your response. I also tried return false but still received the same results. There is obviously something else in the application interfering with the redirect. To minimize cost and time I have gone in another direction.

I appreciate everyone's help but this question should be closed.

Thank you again
CJSantora

Author

Commented:
Expert spent some time trying to help with my issue and was very responsive.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial