6

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.

Eduard Luca
  • 6,514
  • 16
  • 85
  • 137

4 Answers4

4

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

Community
  • 1
  • 1
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
4

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.

2

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.

rss2363
  • 77
  • 2
-1

According to jQuery's API doc (http://api.jquery.com/jQuery.ajax/), async:false (aka. sync mode) does not support cross-domain and dataType: "jsonp" requests.

GlacJAY
  • 159
  • 4
  • 8