Eduard Luca
Eduard Luca

Reputation: 6602

301 redirect on AJAX -- redirected?

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.

Upvotes: 8

Views: 27931

Answers (4)

rss2363
rss2363

Reputation: 77

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.

Upvotes: 2

GlacJAY
GlacJAY

Reputation: 169

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.

Upvotes: -1

Warin Koslowski
Warin Koslowski

Reputation: 75

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.

Upvotes: 4

Pez Cuckow
Pez Cuckow

Reputation: 14412

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

Upvotes: 4

Related Questions