Ajax 重定向处理
我遇到了以前从未遇到过的情况。我有一位客户需要向 URL 发出 ajax 请求。由于内部原因,该 URL 重定向到需要访问其状态代码的另一个 URL。这种多请求场景是由 Ajax 请求原生处理的吗?
使用 jQuery 的快速测试似乎可以处理 302,执行重定向并返回目标页面的内容(我只需要生产中的状态代码,但内容“证明”正在访问正确的页面),但我找不到任何迹象表明我可以期望它普遍适用。我不知道客户将使用什么库(如果有的话)。此外,其他客户端将来可能会使用相同的 URL,因此需要以相同的方式进行处理。
谢谢。
I bumped into a situation I've never had to address before. I have a customer who needs to make an ajax request to a URL. For internal reasons, that URL redirects to another URL whose status code needs to be accessed. Is this kind of multi-request scenario handled natively by Ajax requests?
A quick test using jQuery seems to handle the 302, do the redirection and return the content of the targeted page (I'll only need the status code in production, but the content is what "proves" the correct page is being accessed), but I can't find any indication that I can expect this to work universally. I don't know what, if any, library the client will use. Moreover, other clients are likely to use this same URL in the future and it needs to be handled the same way.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以期望它能够普遍工作,因为它不是 jQuery 进行处理的。这是所有库用于 AJAX 调用的基础
XmlHtpRequest
浏览器对象的指定行为。您可以在此处找到 W3C 指定的行为 :
我在这里强调了最相关的一点,只要它不违反同源政策 规则(它不应该在您的同一域上),XmlHttpRequest 将获取它重定向到的最终页面的内容。换句话说,这就是您现在在 jQuery 中看到的行为...只是不是 jQuery 这样做的。
You can expect this to work universally, since it's not jQuery doing the handling. This is specified behavior of the underlying
XmlHtpRequest
browser object that all libraries use for AJAX calls.You can find the behavior specified here by the W3C:
I've emphasized the most relevant bit here, as long as it doesn't violate same-origin policy rules (it shouldn't on your same domain), the XmlHttpRequest will get the content of final page it gets redirected to. In other words, it's the behavior you're seeing with jQuery now...it's just not jQuery doing it.
如果你查看 firebug,你实际上可以看到重定向的发生。我认为你是对的,你不能在每个浏览器上都依赖它。但我倾向于最新的浏览器会为您处理 http 连接并进行重定向。
If you look at firebug, you can actually see the redirect happening. I think you are right that you can't depend on this for every browser. But my inclination would be that most recent browsers will handle the http connection for you and do the redirect.