I have a webpage with a form. When the user submits the form, I want the server to redirect the browser to another page from the form action. Right now, I am doing this using the PHP header function to send the status code 302. It works fine.
I am trying to make the page on the server redirect the browser in the same way, regardless of whether it was sent normally (without Javascript) or through Ajax. I tried to do this by setting the location of the window to any URL in the Location header. I use jQuery and make the call as follows:
$.ajax({ url: this.action, type: "POST", data: getFormData(this), complete: function(request) { window.location.assign(request.getResponseHeader("Location")); } });
However, this did not work. Thinking about it, I realized that this is not surprising. In an Ajax request, the browser must transparently handle redirection responses, such as 302 code, before modifying readyState. When the full function starts, it searches for the location header at the final destination and does not find it.
As an experiment, I tried to send a 200 status code with the Location header. I tried the Ajax request and it worked fine. However, when I made a non-Ajax submit, this did not work. The browser went to the form action page and stayed there as if it were ignoring the Location header.
Is there a way to do the same page redirect in both cases, without having to know or care about whether the request is an Ajax request?
In this case, I tried the form in different browsers (IE8, IE7, IE6, Firefox 3.5, Chrome) with the same results every time. In addition, I am making a mail request so as not to attack the URL length limit in IE 2083.