博客地址:http://blog.ifootsteps.com/?p=64
在使用ajax请求服务器时,某些情况需要经过身份验证,如果某个用户停留在页面很长时间,导致令牌过期,此时如果访问某些资源,会验证不通过,但页面却不会跳转到登录页面.
这个情况在网上搜索一下发现遇到的人还不少.下面发现几个很好的解释.
The unfortunate truth about AJAX and the 302 redirect is that you can’t get the headers from the return
because the browser never gives them to the XHR. When a browser sees a 302 it automatically applies the redirect. In this case, you would see the header in firebug because the browser got it, but you would not see it in ajax, because the browser did not pass it. This is why the success and the error handlers never get called. Only the complete handler is called.
With the way XHR is built in general (and in Chrome specifically): XHR is not very flexible, and provides a relatively high-level API, with the same behavior the browser has in all other requests (address bar urls, image source urls, embedded script urls), i.e. redirects are handled transparently. No events will be thrown in the JavaScript alerting you of this redirect or the intermediate 302/301 status codes, you will only receive the final status code and data. Therefore, it is impossible to retrieve the “Location” header from the response, as the final response will not contain the “Location” header.