Avatar of 3161
3161
 asked on

Redirection in Javacsript

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

Avatar of undefined
Last Comment
3161

8/22/2022 - Mon
Insoftservice

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";
3161

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

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

var url = data.Url;
window.location.href = url;
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
3161

ASKER
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
ASKER CERTIFIED SOLUTION
Insoftservice

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
3161

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

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Insoftservice

Did u added return false; after redirection code.
3161

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

ASKER
Expert spent some time trying to help with my issue and was very responsive.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck