Question

I'm using GAE webapp2 I have a form that is validated using jquery, then the data is posted using jquery, and in the response of a valid date I'm trying to do a

self.redirect("/thankyou")

it doesn't work, however it actually shows in the terminal that the post method is done and the get method of class Thankyou is also done. the page never change.

can the query file block the response from the server? or am I doing something wrong? the reason why I'm suspecting it has something to do with jquery, is because I have almost the same code on another form that is not validated using jquery and the redirect is working just fine.

here is my jquery code.

//validation code on multiple fileds 
$("#booking_form").validate();

//adding rules to validation
$("#pax_name").rules("add", {
    minlength: 2
});
$("#pax_mobile").rules("add", {
minlength: 10
});

return false;
});

$("#booking_form").validate({
    debug: false,
    submitHandler: function (form) {
        // do other stuff for a valid form
        $.post('/booking', $("#booking_form").serialize(), function (data) {
            Recaptcha.reload();
        });
    }
});

Thank you.

Was it helpful?

Solution

You have to handle the redirect in your Javascript. I hope the following will solve your problem:

$("#booking_form").validate({
    debug: false,
    submitHandler: function (form) {
        // do other stuff for a valid form
        $.ajax({
            type: "POST",
            url: '/booking',
            data: $("#booking_form").serialize(),
            dataType: "json",
            success: function(data) {
                if (data.redirect) {
                    // You can here redirect the user to the new page
                    window.location.href = data.redirect;
                }
            }
        });
    }
});

OTHER TIPS

Since you are firing an AJAX request to the server which will fire asynchronously, you cannot redirect from the server. You need to wait for the response and redirect on the client based on the response sent from the server.

Please look at this link which shows how to handle an ajax redirect from the server.

How to manage a redirect request after a jQuery Ajax call

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top