Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 redirect on AJAX -- redirected?

Tags:

redirect

ajax

I have an AJAX call to a server endpoint that does a 301 redirect to the same page, but with a trailing slash.

Does the browser follow redirects when called with AJAX or does it ignore them? In my case it doesn't follow them, but I figured it might be something from the server config.

like image 866
Eduard Luca Avatar asked Sep 12 '12 10:09

Eduard Luca


People also ask

Do Ajax calls follow redirects?

ajax appears to always follow redirects. How can I prevent this, and see the redirect without following it? There are various questions with titles like "jquery ajax redirect" but they all appear to involve accomplishing some other goal, rather than just directly checking the status that a server gives.

How do I redirect to another page in Ajax?

$. ajax({ type: 'POST', url: 'AJAX URL', data: "YOUR DATA" // don't forget to pass your csrf_token when using post success: function(data){ $("what_ever_you_want_to_replace"). html(data. view); }, error: function(xhr, type, exception) { // if ajax fails display error alert alert("ajax error response type "+type); } });

Does 301 automatically redirect?

301 is an HTTP status code sent by a web server to a browser. A 301 signals a permanent redirect from one URL to another, meaning all users that request an old URL will be automatically sent to a new URL.


3 Answers

If you are using jquery you could look at the questions below to implement it. By default jQuery (and most libraries with Ajax) don't follow redirects by default:

How to manage a redirect request after a jQuery Ajax call

How to prevent ajax requests to follow redirects using jQuery

handle jquery ajax redirect

like image 85
Pez Cuckow Avatar answered Oct 17 '22 01:10

Pez Cuckow


Maybe this answer is a little bit late but i had the same problem with 301 response on ajax request. The solution was quite simple:

apache rewrite rule is something like this:

RewriteRule ^([^/]\w+)/?$ index.php?%{QUERY_STRING} [L,E=MODULE:$1]

Your XHR-Request url looks someting like this:

/this/is/a/canonical/url + '?param=1&param=2...'

It will lead to the 301 moved permanently if you dont use a direct file call (f.i. *.php) and rewrite to canonical URL (looks like a directory-path without f.i. *.php) instead.

To solve this problem just add a / to your XHR-Request-URL like this:

/this/is/a/canonical/url + '/' + '?param=1&param=2...'

Maybe this will help someone.

like image 4
Warin Koslowski Avatar answered Oct 17 '22 01:10

Warin Koslowski


I also had this problem and the suggestion about the trailing slash got me thinking ... I had a rewrite rule in my Web.Config to make everything lowercase and that's what was messing up my AJAX call. I was POSTing to GetResults (which showed up as a 301) and my rewriter (for some unknown reason?) was changing it to a lower-cased getresults GET which resulted in a 404.

Hope this might help someone else.

like image 2
rss2363 Avatar answered Oct 17 '22 01:10

rss2363