jQuery Ajax Call Getting the Redirect Content Problem

by heru on January 17, 2011

When submitting a form, we will usually get redirected to the “thank you” page. During this process, the header given out at the target page is 302 redirect.

So, what has this got to do with jQuery AJAX Call?

We might have used jQuery AJAX call to submit a form and this form redirects it to the thank you page.

For instance:

$.ajax({ 'type' : 'POST', data: data, url: 'submit.php', complete: function (xhr, status) { $('#content').html(xhr.responseText); });

where submit.php will redirect its page to thankyou.php.

However, when we tried using the above code, the page did get redirected, but the on complete function did not get called. So, $('#content') did not get updated.

To solve this, basically all we need is to add async: false to the ajax option. So, it will be like:

$.ajax({ 'type' : 'POST', data: data, url: 'submit.php', async: false, complete: function (xhr, status) { $('#content').html(xhr.responseText); });

~voila. The problem is fixed. Now, $('#content') gets updated correctly.

Related Posts

  • No Related Post
  • Tel

    aku unto

Previous post:

Next post: