如何赶上预期(和意)302 Ajax响应? [英] How to catch an expected (and intended) 302 Ajax response?

查看:118
本文介绍了如何赶上预期(和意)302 Ajax响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,如果你回头看看我的<一个href="http://stackoverflow.com/questions/2764444/getting-autodiscover-url-from-exchange-email-address">$p$pvious有关Exchange自动发现问题,你会看到easiet的方式来获得自动发现URL是发送一个非安全,非认证 GET 请求服务器,鼻翼:

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

该服务器会返回一个302重定向响应与正确的URL中的位置头。

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

我想了很简单的东西先是用Chrome扩展,在那里我有:

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");
}

使用另一个Ajax调用设置了完整的XML邮政与用户凭据,

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

但是,Chrome浏览器,而不是挂在这一点上,一看开发商面板表明,它没有返回回响应,而是在演戏一样,没有回应给予,同时呈现出

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

在错误日志。

我看到它,指的是精确的响应状态的方法是对的追赶是一样的,但我不知道问题是否与铬/ WebKit的或者这是怎么XHR请求总是处理重定向。

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.

或者才有可能建立一个二级XHR这样,当它得到的302,它发出了一个完全不同的要求?

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

我只是改变了它,使它不检查响应code:

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

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

,而是当我提醒了值,这是。并且还有同样的错误,并在开发控制台没有任何反应。所以它看起来像它要么不跟踪302响应为响应,或有事可以擦拭之后的反应呢?

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?

推荐答案

见的在XHR W3C的文档:在XHR要么透明跟随重定向或返回网络错误(比如你看到从浏览器什么的),根据重定向的目的地是在相同的原点作为原来的要求。所以,你的code真的没有机会赶上在所有的302响应。这就是为什么位置响应头未设置:在这一点上,你检查的最终响应的头,而不是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.

这篇关于如何赶上预期(和意)302 Ajax响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆