如何获得jQuery.ajax响应状态? [英] How to get jQuery.ajax response status?

查看:546
本文介绍了如何获得jQuery.ajax响应状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道是否jqXHR对象返回从另一个请求被重定向?例如,得到具有下列code:

Is there any way to know if the jqXHR object returned was redirected from another request? For example, having the following code:

   jQuery.ajax({
            type:       'POST',
            url:        'http://example.com/page.html',
            complete:   function(response, status) {
                        console.log(response.statusCode);
                        // do something
                        }
        });

如果阿贾克斯URL重定向和301或302状态code时,反应会重定向的页​​面的结果,将返回200(OK)状态code。我认为这是一个jQuery的错误(真的不知道这是否是预期的beheavior,因为最终的反应实际上是成功的) 我不能既得到原始的响应头,以检查是否存在一个Location头,因为这是仅适用于重定向的请求。

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.

萤火虫上述code记录通过XHR这两个请求

Firebug with the above code logs these two requests through XHR

http://example.com/page.html 302(临时移动) http://example.com/redirect.html 200(OK)

http://example.com/page.html 302 (Moved temporarily) http://example.com/redirect.html 200 (OK)

但jQuery的只是从第二个请求返回的数据。

But jQuery only return data from the second request.

也许有一种方法,以从第一请求获得的数据?或者获得响应URL,并将其与原始请求一个比较?

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?

推荐答案

问题是,301或302直接处理由浏览器,由于标准的兼容性。此状态code未传递到你的Ajax调用。你知道调用完成的唯一方式是在完成处理程序,此外,还有传递完成jqxhr对象没有给出任何有用的状态。成功和错误处理程序将永远不会被调用。

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.

我们有一些遗留的红宝石露营code是用来重定向到get()方法时,post()方法被调用,这样我们就可以显示通过资源的红宝石更新视图。当我们切换到JavaScript的意见,从露营意见,301/302并没有传递到jqxhr对象。我们不得不重写后的方法返回200来解决该问题。

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.

下面是一些关于这个问题的StackOverflow的帖子。

Here are some stackoverflow posts on the subject.

<一个href="http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call">How管理一个jquery Ajax调用后重定向请求

<一个href="http://stackoverflow.com/questions/373087/catching-302-found-in-javascript/1534604#1534604">Catching 302发现的JavaScript

<一个href="http://stackoverflow.com/questions/1393280/http-redirect-301-vs-302-temporary-vs-permanent/1393298#1393298">http重定向:301与302(临时与永久)

这篇关于如何获得jQuery.ajax响应状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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