AJAX 上的 301 重定向 - 重定向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12386272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
301 redirect on AJAX -- redirected?
提问by Eduard Luca
I have an AJAX call to a server endpoint that does a 301 redirect to the same page, but with a trailing slash.
我有一个对服务器端点的 AJAX 调用,该端点执行 301 重定向到同一页面,但尾部有斜杠。
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.
使用 AJAX 调用时浏览器是否遵循重定向或忽略它们?在我的情况下,它不遵循它们,但我认为它可能来自服务器配置。
采纳答案by Pez Cuckow
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 followredirects by default:
如果您正在使用 jquery,您可以查看下面的问题来实现它。默认情况下,jQuery(以及大多数带有 Ajax 的库)默认不遵循重定向:
How to manage a redirect request after a jQuery Ajax call
How to prevent ajax requests to follow redirects using jQuery
回答by Warin Koslowski
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:
也许这个答案有点晚了,但我对 ajax 请求的 301 响应有同样的问题。解决方法很简单:
apache rewrite rule is something like this:
apache 重写规则是这样的:
RewriteRule ^([^/]\w+)/?$ index.php?%{QUERY_STRING} [L,E=MODULE:]
Your XHR-Request url looks someting like this:
你的 XHR-Request url 看起来像这样:
/this/is/a/canonical/url + '?param=1¶m=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.
如果您不使用直接文件调用 (fi *.php) 并重写为规范 URL(看起来像没有 fi *.php 的目录路径),它将导致 301 永久移动。
To solve this problem just add a / to your XHR-Request-URL like this:
要解决这个问题,只需在 XHR-Request-URL 中添加一个 / ,如下所示:
/this/is/a/canonical/url + '/' + '?param=1¶m=2...'
Maybe this will help someone.
也许这会帮助某人。
回答by rss2363
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.
我也有这个问题,关于尾部斜杠的建议让我思考......我在我的 Web.Config 中有一个重写规则,将所有内容都设为小写,这就是我的 AJAX 调用混乱的原因。我正在向 GetResults 发布(显示为 301),而我的重写器(出于某种未知原因?)正在将其更改为小写的 getresults GET,这导致了 404。
Hope this might help someone else.
希望这可以帮助别人。
回答by GlacJAY
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.
根据 jQuery 的 API 文档(http://api.jquery.com/jQuery.ajax/),async:false(又名同步模式)不支持跨域和dataType: "jsonp"请求。

