My POST request handler can either return content that I load into a div or the browser is redirected. When I use jQuery's .post()
, in the case of a redirect, instead of the entire page redirecting, the content returned from the redirect is loaded into the div:
var request = $.post('/my-post-url/');
request.done(function(response) {
$('#my-div').empty();
$('#my-div').append(response); // In case of redirect, response has new page's contents
});
To get around this, I use the code from this answer:
request.done(function(response) {
if(xhr.responseURL != window.location.href.match(/(^[^#]*)/)[0])
{
location.href = xhr.responseURL;
return;
}
$('#my-div').empty();
$('#my-div').append(response);
});
This works fine, but I end up doing two GET
requests for the same content: the first one is done automatically by the time I get inside the done
handler, and the second is when I set location.href
. Is there a way to either prevent this extra GET request and/or redirect the ENTIRE page on a redirect?