如何捕获预期(和预期)的 302 Ajax 响应?

发布于 2024-08-31 16:46:12 字数 1253 浏览 1 评论 0 原文

因此,如果您回顾一下我关于 Exchange 自动发现的上一个问题,您会发现,获取自动发现 URL 的最简单方法是向服务器发送一个不安全、未经身份验证的 GET 请求,例如:

http://autodiscover.exchangeserver.org/autodiscover/autodiscover.xml

服务器将响应 302 重定向,并在Location 标头。

我首先使用 Chrome 扩展尝试一些非常简单的东西,其中我有:使用

if (req.readyState==4 && req.status==302) {
    return req.getResponseHeader("Location");
}

完整的 XML Post 和用户凭据设置另一个 ajax 调用,

但 Chrome 此时挂起,并查看开发人员面板显示它没有返回响应,而是表现得像没有给出响应,同时

Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101

在错误日志中显示 。

在我看来,引用确切的响应状态与“捕获”它大致相同,但我不确定问题是否出在 Chrome/WebKit 上,或者这是否是 XHR 请求始终处理重定向的方式。

我不知道如何捕获这个,以便我仍然可以从响应中获取标头。

或者是否可以设置一个辅助 XHR,以便当它收到 302 时,它会发送一个完全不同的请求?

快速更新

我刚刚更改了它,以便它不检查响应代码:

if (req.readyState==4) {
    return req.getResponseHeader("Location");
}

相反,当我警告该值时,它是 null。并且开发控制台中仍然存在相同的错误并且没有响应。所以看起来它要么不跟踪 302 响应作为响应,要么在清除该响应之后发生了一些事情?

So, if you look back at my previous question about Exchange Autodiscover, you'll see that the easiet way to get the autodiscover URL is to send a non-secure, non-authenticated GET request to the server, ala:

http://autodiscover.exchangeserver.org/autodiscover/autodiscover.xml

The server will respond with a 302 redirect with the correct url in the Location header.

I'm trying out something really simple at first with a Chrome extension, where I have:

if (req.readyState==4 && req.status==302) {
    return req.getResponseHeader("Location");
}

With another ajax call set up with the full XML Post and the user credentials,

But instead Chrome hangs at this point, and a look at the developer panel shows that it is not returning back the response but instead is acting like no response was given, meanwhile showing a

Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101

in the error log.

The way I see it, refering to the exact response status is about the same as "catching" it, but I'm not sure if the problem is with Chrome/WebKit or if this is how XHR requests always handle redirects.

I'm not sure how to catch this so that I can get still get the headers from the response.

Or would it be possible to set up a secondary XHR such that when it gets the 302, it sends a totally different request?

Quick Update

I just changed it so that it doesn't check the response code:

if (req.readyState==4) {
    return req.getResponseHeader("Location");
}

and instead when I alert out the value it's null. and there is still the same error and no response in the dev console. SO it seems like it either doesn't track 302 responses as responses, or something happens after that wipes that response out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

埋葬我深情 2024-09-07 16:46:12

请参阅有关 xhr 的 w3c 文档:XHR将透明地遵循重定向或返回网络错误(就像您从 Chrome 中看到的那样),具体取决于重定向的目标是否与原始请求位于同一来源。所以你的代码根本没有机会捕获 302 响应。这就是未设置“Location”响应标头的原因:此时,您正在检查最终响应的标头,而不是 302 响应。

See the w3c docs on xhr: the XHR will either transparently follow the redirect or return a network error (like what you're seeing from Chrome), depending on whether the redirect's destination is in the same origin as the original request. So your code really doesn't have a chance to catch the 302 response at all. That's why the "Location" response header is unset: at this point, you're inspecting the headers of the final response, not the 302 response.

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