找出jQuery的Ajax请求被重定向到

找出jQuery的Ajax请求被重定向到

问题描述:

所以 - 我有这个Ajax请求,见式。金发,大约6英尺高,看起来是这样的:

So- I've got this ajax request, see-. Blonde, about 6 feet tall, looks like this:

$.ajax({
    url: 'http://example.com/makeThing',
    dataType: 'html',
    type: 'POST',
    data: {
        something:someotherthing
    },
    complete: function(request, status) {
        console.log("headers=" + request.getAllResponseHeaders(););
    }
});

什么情况是,请求/ makeThing返回302重定向到第二个URL:getThing / ABC123。所有我想要做的就是找​​出这第二个网址是(programmatically-它会每次都不同,所以只检查萤火并不能帮助我)。我试过管道是回来了完整的回调响应头,但只是给了我什么回来的第二个请求。

What happens is that the request to '/makeThing' returns a 302 redirect to a second url:'getThing/abc123'. All I want to do is find out what that second url is (programmatically- it'll be different every time, so just checking in firebug doesn't help me). I've tried plumbing the response headers that come back to the 'complete' callback, but that just gives me what came back on the second request.

约束: - 我已经在这个运行导通只是JS服务器无法控制。 如果我要 - 可切换框架(道场?原型?)

Constraints: -I have no control over the server this is running on- just the js. -Can switch frameworks if I have to (dojo? prototype?)

在理想情况下,我会做某种仅标头请求/ makeThing找出重定向URL是通过获取初始302的反应仅仅是头什么。

Ideally, I'd do some sort of header-only request to /makeThing to find out what the redirect url is by getting just the headers of the initial 302 response.

如果做不到这一点(因为jQuery的自动遵循重定向和似乎并没有有办法的要求之间的介入),我得到恢复的最终响应,并使用它以某种方式得到...什么网址?请求对象,也许?

Failing that (since jquery auto-follows redirects and doesn't seem to have a way to step in between the requests), I get retrieve the final response and use it to somehow get the url from... something? The request object, maybe?

TLDR:Ajax请求。框架自动跟随造成302重定向。我如何找出它重定向到?

TLDR: Sending an ajax request. Framework auto-follows the resulting 302 redirect. How do I figure out where it redirected to?

编辑,澄清: 最终的URL将是不同的每一个时间呼叫makeThing导致服务器创建被托管后,在getThing / ABC123

EDIT, Clarification: The final url will be different every time- calling 'makeThing' causes the server to create what is thereafter hosted at 'getThing/abc123'

如果有可能,您的服务器设置一个标题的页面(目标页面,而不是重定向一个)上,例如设置 当前定位头,那么你可以这样做:

If possible, have your server set a header on those pages (the destination pages, not the redirecting one), for example set a "current-location" header, then you can do this:

$.ajax({
    url: 'http://example.com/makeThing',
    dataType: 'html',
    type: 'POST',
    data: {
        something:someotherthing
    },
    complete: function(request, status) {
        var location = request.getResponseHeader("current-location");
    }
});

是的,这的的有点哈克,但由于重定向内部处理的XmlHtt prequest对象(事件,你不能访问),你没有太多的选择。

Yes, this is a bit hacky, but since the redirect is handled internally in the XmlHttpRequest object (an event to which you can't access), you don't have much choice.

您可以查看规格,这是的的为XmlHtt prequest行为:

You can check the spec, this is the intended behavior for XmlHttpRequest:

如果通过Location头所传达的URL的起源是相同的起源与XMLHtt prequest起源和重定向不违反无限循环precautions,透明地跟随重定向同时观察同源请求事件规则。

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.