browsers don't resend form data in ajax call if they are redirected?
I've been tinkering with URL Rewriting on my site and fixing the canonicalization of my URLs (so I don't have duplicate data on search engines, etc.) and it was recommended everywhere to either always remove/force the trailing slash (I chose to remove it) and also to redirect /index to . So I put the following two rules in my .htaccess for Apache's mod_rewrite to pick up:
--remove trailing slash
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
--remove trailing /index
RewriteRule ^(.*)/?index$ http://%{HTTP_HOST}/$1 [R=301,L]
These rules work just fine in the browser. But some of my ajax forms stopped functioning all of a sudden! I traced it both in Chrome and Firefox(Firebug) and what happens is very strange. In both browsers, the page does an ajax call to:
<mydomain>/ajax/index (ajax params intact) --> 301 redirect to <mydomain>/ajax/
<mydomain>/ajax/ (ajax params are no longer sent!) --> 301 redirect to <mydomain>/ajax
<mydomain>/ajax (ajax params again not sent) --> 200 OK but obviously script complains that params don't exist
Can anyone confirm that this is the case or am I losing it? If so, why the heck does it behave like this? And is there a proper solution for fixing it开发者_StackOverflow社区 (other than changing all my ajax calls to never get redirected eg <mydomain>/ajax
)?
Thank you very much for you time guys :)
Cheers
Ali
The HTTP specification says that a browser should resend the parameters along with the redirected request for a 301 redirect. On the other hand, it also says that the browser should manually confirm with the user that it's OK to resend those parameters each time the request is redirected. That kind of defeats the point of AJAX, if the user gets two dialog boxes popping up every time he/she submits your form. So for that reason, if nothing else, I'd recommend setting up your server-side scripts so that there will be no redirects on AJAX calls.
In reality, browsers tend to ignore the part of the specification that tells them how to handle a redirect on a form submission. So you're probably not crazy; it seems perfectly believable that the behavior you're seeing is built into the browsers intentionally (or maybe because someone was too lazy to code it the right way).
精彩评论