Is there any way to know if the jqXHR object returned was redirected from another request? For example, having the following code:
有没有办法知道返回的jqXHR对象是否从另一个请求重定向?例如,拥有以下代码:
jQuery.ajax({
type: 'POST',
url: 'http://example.com/page.html',
complete: function(response, status) {
console.log(response.statusCode);
// do something
}
});
If the ajax url is redirected with a 301 or 302 status code, the response will be the result of the redirected page and will return a 200 (OK) status code. I think this is a jQuery's bug (don't really know if this is the intended beheavior, since the ultimate response was actually succesful) I can't either get the original response Headers to check if there exists a Location Header, since this is only available on redirected requests.
如果使用301或302状态代码重定向ajax url,则响应将是重定向页面的结果,并将返回200(OK)状态代码。我认为这是一个jQuery的错误(不知道这是否是预期的恶作剧,因为最终的响应实际上是成功的)我不能得到原始响应Headers来检查是否存在Location Header,因为这是仅适用于重定向的请求。
Firebug with the above code logs these two requests through XHR
带有上述代码的Firebug通过XHR记录这两个请求
http://example.com/page.html 302 (Moved temporarily) http://example.com/redirect.html 200 (OK)
http://example.com/page.html 302(暂时移动)http://example.com/redirect.html 200(确定)
But jQuery only return data from the second request.
但是jQuery只返回第二个请求的数据。
Maybe there is a way to get data from the first request? Or to get the response url and compare it with the one from the original request?
也许有办法从第一个请求中获取数据?或者获取响应URL并将其与原始请求中的URL进行比较?
2 个解决方案
#1
2
The issue is that a 301 or 302 is handled directly by the browser due to standards compliance. This status code is not passed on to your ajax call. The only way that you know that the call has completed is in the complete handler, and furthermore there is no useful status given in the jqxhr object passed to complete. The Success and Error handlers will never get called.
问题在于,由于符合标准,浏览器直接处理301或302。此状态代码不会传递给您的ajax调用。您知道调用已完成的唯一方法是在完整的处理程序中,此外,传递给complete的jqxhr对象中没有给出有用的状态。 Success和Error处理程序永远不会被调用。
We had some legacy ruby camping code that used a redirect to the get() method when a post() method was called so that we could display an updated view via ruby of the resource. When we switched to JavaScript views from camping views, the 301/302 was not passed to the jqxhr object. We had to rewrite the post methods to return 200 to fix the issue.
我们有一些遗留的ruby野营代码,当调用post()方法时,它使用重定向到get()方法,以便我们可以通过资源的ruby显示更新的视图。当我们从露营视图切换到JavaScript视图时,301/302没有传递给jqxhr对象。我们不得不重写post方法以返回200来解决问题。
Here are some * posts on the subject.
以下是该主题的一些*帖子。
How to manage a redirect request after a jQuery Ajax call
如何在jQuery Ajax调用之后管理重定向请求
Catching 302 FOUND in JavaScript
在JavaScript中捕获302 FOUND
HTTP redirect: 301 (permanent) vs. 302 (temporary)
HTTP重定向:301(永久)与302(临时)
#2
0
A tool i found that might possibly be able to help is wireshark. It's free and may be able to find out what is happening with the jqXHR object. It's fairly easy to use but follow these instructions:
我发现可能能够提供帮助的工具是wireshark。它是免费的,可能能够找出jqXHR对象发生了什么。这很容易使用,但请遵循以下说明:
http://www.wireshark.org/ Wireshark is a network protocol analyzer for Unix and Windows. I have only brushed the surface with this tool so far but it comes in handy when performing analysis on requests between your machine and others on the network.
http://www.wireshark.org/ Wireshark是适用于Unix和Windows的网络协议分析程序。到目前为止,我只使用此工具刷了表面,但在对机器与网络上的其他人之间的请求进行分析时,它会派上用场。
Before you start - Wireshark captures every request so only click start when you are ready to capture and click stop when your finished. - You'll need your IP address
在开始之前 - Wireshark捕获每个请求,因此只有在准备捕获时单击“开始”,并在完成后单击“停止”。 - 你需要你的IP地址
Read more about analysing HTTP requests
阅读有关分析HTTP请求的更多信息
#1
2
The issue is that a 301 or 302 is handled directly by the browser due to standards compliance. This status code is not passed on to your ajax call. The only way that you know that the call has completed is in the complete handler, and furthermore there is no useful status given in the jqxhr object passed to complete. The Success and Error handlers will never get called.
问题在于,由于符合标准,浏览器直接处理301或302。此状态代码不会传递给您的ajax调用。您知道调用已完成的唯一方法是在完整的处理程序中,此外,传递给complete的jqxhr对象中没有给出有用的状态。 Success和Error处理程序永远不会被调用。
We had some legacy ruby camping code that used a redirect to the get() method when a post() method was called so that we could display an updated view via ruby of the resource. When we switched to JavaScript views from camping views, the 301/302 was not passed to the jqxhr object. We had to rewrite the post methods to return 200 to fix the issue.
我们有一些遗留的ruby野营代码,当调用post()方法时,它使用重定向到get()方法,以便我们可以通过资源的ruby显示更新的视图。当我们从露营视图切换到JavaScript视图时,301/302没有传递给jqxhr对象。我们不得不重写post方法以返回200来解决问题。
Here are some * posts on the subject.
以下是该主题的一些*帖子。
How to manage a redirect request after a jQuery Ajax call
如何在jQuery Ajax调用之后管理重定向请求
Catching 302 FOUND in JavaScript
在JavaScript中捕获302 FOUND
HTTP redirect: 301 (permanent) vs. 302 (temporary)
HTTP重定向:301(永久)与302(临时)
#2
0
A tool i found that might possibly be able to help is wireshark. It's free and may be able to find out what is happening with the jqXHR object. It's fairly easy to use but follow these instructions:
我发现可能能够提供帮助的工具是wireshark。它是免费的,可能能够找出jqXHR对象发生了什么。这很容易使用,但请遵循以下说明:
http://www.wireshark.org/ Wireshark is a network protocol analyzer for Unix and Windows. I have only brushed the surface with this tool so far but it comes in handy when performing analysis on requests between your machine and others on the network.
http://www.wireshark.org/ Wireshark是适用于Unix和Windows的网络协议分析程序。到目前为止,我只使用此工具刷了表面,但在对机器与网络上的其他人之间的请求进行分析时,它会派上用场。
Before you start - Wireshark captures every request so only click start when you are ready to capture and click stop when your finished. - You'll need your IP address
在开始之前 - Wireshark捕获每个请求,因此只有在准备捕获时单击“开始”,并在完成后单击“停止”。 - 你需要你的IP地址
Read more about analysing HTTP requests
阅读有关分析HTTP请求的更多信息