Ajax 重定向处理

发布于 2024-10-02 22:51:57 字数 280 浏览 0 评论 0原文

我遇到了以前从未遇到过的情况。我有一位客户需要向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芯好空 2024-10-09 22:51:57

可以期望它能够普遍工作,因为它不是 jQuery 进行处理的。这是所有库用于 AJAX 调用的基础 XmlHtpRequest 浏览器对象的指定行为。

您可以在此处找到 W3C 指定的行为 :

如果 来源 .w3.org/TR/XMLHttpRequest/#url" rel="noreferrer">Location 标头传达的 URL 是 相同来源XMLHttpRequest origin 并且重定向不违反无限循环预防措施,透明地遵循重定向,同时观察同源请求事件规则

我在这里强调了最相关的一点,只要它不违反同源政策 规则(它不应该在您的同一域上),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:

If the origin of the URL conveyed by the Location header is same origin with the XMLHttpRequest origin and the redirect does not violate infinite loop precautions, transparently follow the redirect while observing the same-origin request event rules.

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.

李白 2024-10-09 22:51:57

如果你查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文