0

I am making an AJAX call to a controller, and I want to redirect to a new page on Success. So I have have the redirection logic within my success callback function. But it doesn't redirect to a new page instead it stays on the same page.

The intriguing thing is that a GET Request (with the form Data I just sent via POST) is made after the Ajax Callback is executed. that is, I see a GET Request in the Address Bar.

Here is my AJAX request

$.ajax({
        url: "processData",
        type: "POST",
        dataType: 'json',
        contentType:'application/json',
        async: false,
        data: JSON.stringify(req),
        success: function(result) {
            url = window.location.href;
            url = url.replace("processData", "getMoreData");
            alert(url); // correct url is printed
            window.location.replace(url);
        },
        error: function() {
            alert("--- Failure ---");
        }
    });

What's wrong in here?

AgentX
  • 1,402
  • 3
  • 23
  • 38
  • add location.reload as last statement after changing the url. I think it should work. – NightsWatch Oct 05 '15 at 09:36
  • @PradeepSekar Thanks Pradeep, but it still doesn't work. – AgentX Oct 05 '15 at 09:50
  • I am wondering! once you have the url in window when you reload it manually does it takes you to where you are expecting? – NightsWatch Oct 05 '15 at 09:54
  • @PradeepSekar the url in the window is not the changed URL. It is giving the same URL and appending it with the POST parameters (like a GET Request) – AgentX Oct 05 '15 at 10:55
  • 1
    window.location = url may work for you. I found a similar question here, pls check: http://stackoverflow.com/questions/846954/change-url-and-redirect-using-jquery – NightsWatch Oct 05 '15 at 11:05
  • @PradeepSekar thanks, all these answers point to the same solution. Don't know what's wrong with my solution, somehow it's just now redirecting. – AgentX Oct 05 '15 at 11:14
  • window.location = url didnt work? – NightsWatch Oct 05 '15 at 11:14
  • @PradeepSekar nope. I think that there is a problem somewhere else. But I don't know where to look. on submit, control comes into my success callback and then there is a GET Request made, don't know from where. – AgentX Oct 05 '15 at 11:19

2 Answers2

0

And what is wrong with

window.location.href='some url';

That is one thing. Another one - You should remove 'spring-mvc' tag from the question because it is not actually Spring-related.

Chlebik
  • 646
  • 1
  • 9
  • 27
  • I already tried it. Still it did not redirect to the new URL. The URL remained the same and there were the form params appended with the URL now. Like localhost:8000/processData?name=xyz&value=abc – AgentX Oct 05 '15 at 10:57
  • Hmmm, I have same thing in one of my apps I am maintaining, but not with jQuery ajax call but after logging in with normal POST request (putting form data to URL) but the redirect itself works. I think You should try it in different browsers and also check if it works in DONE handler + check if something changes when ASYNC is set to TRUE. And also (seeing Your answer above) - show Your controller method. I assume You are not redirecting in controller? – Chlebik Oct 05 '15 at 11:50
  • I tried using Firebug in Firefox. The interesting issue is that when I put a break point in the success callback & step over each line it first prints an alert box and then redirects. But when I do not execute line by line & just run it, it does not redirect. – AgentX Oct 05 '15 at 12:02
  • So try what I've recommended and also remove alert and replace it with console.log. – Chlebik Oct 05 '15 at 12:12
  • Replaced alert with console.log and it started working in chrome (no idea why?) but still doesn't work in Firefox. – AgentX Oct 05 '15 at 13:20
  • And if You remove logging and alert does it change anything? – Chlebik Oct 05 '15 at 13:28
  • I found the issue, it was because of both GET & POST requests being generated. – AgentX Oct 05 '15 at 16:59
0

The answer posted by @Chlebik above is correct. The main issue I was facing was that both GET & POST requests were getting generated. So sometimes the GET request was going forward & the POST request was getting cancelled. Other times the reverse was happening & I was getting correct result.

All I had to do was to avoid the GET request, from the default form's Submit button, from getting generated.

Here's the code for that:

$('form').on('submit', function(e) {
    e.preventDefault();

I have taken the code fhttp://stackoverflow.com/questions/32953573/form-processing-via-ajax-avoiding-both-get-post-requests-from-getting-genera

AgentX
  • 1,402
  • 3
  • 23
  • 38