Note that there are some explanatory texts on larger screens.

plurals
  1. POForm that makes browser redirect when accessed by either a regular form submit or an Ajax request - is this possible?
    primarykey
    data
    text
    <p>I have a web page with a form. When the user submits the form, I want the server to make the browser redirect to a different page from the form action. Right now, I am doing this by using PHP's <a href="http://us2.php.net/manual/en/function.header.php" rel="nofollow noreferrer">header</a> function to send a 302 status code. It works fine.</p> <p>I am trying to make the page on the server redirect the browser in the same way, regardless of whether it was submitted normally (without Javascript) or via Ajax. I tried to do this by setting the window location to whatever URL is in the Location header. I am using jQuery, and doing a call like this:</p> <pre><code>$.ajax({ url: this.action, type: "POST", data: getFormData(this), complete: function(request) { window.location.assign(request.getResponseHeader("Location")); } }); </code></pre> <p>However, that didn't work. After thinking about it, I realized that this is not very surprising. In an Ajax request, the browser is supposed to transparently handle redirect responses such as 302 codes before changing the readyState. When the complete function runs, it is looking for the Location header in the final destination and not finding it.</p> <p>As an experiment, I then tried sending a 200 status code with a Location header. I tried the Ajax request and it worked fine. However, when I did the non-Ajax submit, it didn't work. The browser went to the form action page and stayed there, like it was ignoring the Location header.</p> <p>Is there any way to make the same page redirect in both cases, without the server having to know or care whether the request is an Ajax request?</p> <p>In case this matters, I tried the form in various browsers (IE8, IE7, IE6, Firefox 3.5, Chrome) with similar results each time. Also, I am doing a post request to avoid bumping into IE's 2083-character URL length limit.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload