-1

This is the sample code I am using to hit a URL with fixed time interval.

$(document).ready(function() {
    var counter = 1;

    $.doTimeout( 1000, function() {
        $.ajax({
            type: "GET",
            url: "<%=encodeUrl%>",
            timeout: 10000,
            dataType: "text",

            complete: function(resp) {
                if (resp.status == 200) {
                    $("#msg").html(counter++);
                } else {
                    $("#msg").html("Failed");
                    return true;
                }
            }
        });
    });
});

Target URL is a servlet which is forwarding the control to another JSP. As per my understanding I must be redirected to new page. But it is showing the same page with the counter value 1. Means, redirect from target servlet is not working. And response is coming back to the same page.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90
  • My servlet do nothing but check DB values. If value present then redirect to another JSP page. Otherwise return back to calling page – Amit Kumar Gupta Mar 24 '11 at 04:07

3 Answers3

2

When your AJAX response is a redirect to another page, the redirected page will be fetched as the response of your AJAX request, that's why your are getting only 200 as HTTP status.

You cannot handle redirects based on the HTTP Status codes that you receive with AJAX.

An AJAX response cannot redirect you to a different page unless you program it to do so.

So if you want to redirect based on your AJAX response, you should modify your server side code to send you the redirect URL as a response, rather than redirecting.

Refer one of answers with example solution

Community
  • 1
  • 1
Livingston Samuel
  • 2,422
  • 2
  • 20
  • 35
  • Note that "forward" is a specific JSP/Servlet term which means that the controller servlet is been instructed to use the specified JSP file as view. There's no means of a HTTP 3nn redirect here. – BalusC Mar 23 '11 at 13:58
0

AJAX isn't meant to redirect. These headers don't get executed by your browser thus letting you stay on that page! What is the exact code your servlet gives back?

Thorben
  • 6,871
  • 2
  • 19
  • 19
  • Well! on your suggession, I had removed intermediate page. I have placed ajax code on the JSP where i need to redirect. If everything is fine, i show page contents. Otherwise show appropriate page. Thanks – Amit Kumar Gupta Mar 24 '11 at 04:09
0

The code is doing exactly what its written to do. You are firing an ajax call, and on response 200 you are setting counter as html for #msg. There is nothing in the code that'll make you redirect to the New Page.

You do not need ajax here if you want to redirect. Else, if your redirect is based on response returned by the servlet, capture it in complete and set window.location.href = 'your/redirect/url/' to load the New Page.

simplyharsh
  • 35,488
  • 12
  • 65
  • 73
  • window.location.href is a good option. But i can't use it since the url is encoded at run time. And some dynamic parameters are added to the URL. – Amit Kumar Gupta Mar 24 '11 at 03:56