`

当Ajax请求遇到 redirect 跳转

阅读更多

博客地址: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.

单词都很简单易懂.另share一下 原文地址:

AJAX redirect dilemma, how to get redirect URL OR how to set properties for redirect request
jQuery and AJAX response header

 

所以无奈下搞出一个方法.

在我们的业务场景下,在服务器端接收到请求后,可以区分是否为ajax请求,所以在区别出是否为ajax请求后,设置如下代码:

//如果是页面请求直接设置为转发,否则为ajax请求设置返回头信息
        if(per.value()==PermissionPolicy.PAGE){
            response.sendRedirect(redirectURL);
        }else{
            response.setIntHeader("REQUIRES_AUTH",1);
            response.setHeader("REQUIRES_AUTH_URL",redirectURL);
        }
        return null;

 在页面因为ajax请求本身就是我们自己封装好的所以只需要加上error时候处理就ok了

$.ajax({
    url : url,
    beforeSend : function(request) {
        request.setRequestHeader("uri", window.location.href);
    },
    type : 'get',
    data : query,
    dataType : 'json',
    error : function( req, status, err) {
        var auth = req.getResponseHeader("REQUIRES_AUTH");
        var auth_url = req.getResponseHeader("REQUIRES_AUTH_URL");
        if(auth == 1 && auth_url){
            window.location.href = auth_url;
        }
        callback([
            {
                datas: '',
                count: 0
            }
        ]);
    },
    success : function(result, textStatus, request) {
        callback(result);
    }
});

 

 

最后分享一句讨厌的话..

Sorry — I hate the “you can’t do this” answers, too, but there are some JavaScript limitations that haven’t quite been ironed out yet.

 

如果其他有解决办法求分享..

分享到:
评论

相关推荐

    simple-res:节点 ajax 或重定向的简单响应

    simple-res简化同时需要应付 ajax和http的请求, 例如删除一个记录,如果是http请求,返回用户删除成功信息,并跳转到一个页面,或者ajax请求,直接返回删除成功依赖模块1. simple-res依赖express和session安装npm ...

    千方百计笔试题大全

    170、Javascript如何发送一个Ajax请求? 41 171、AJAX都有哪些有点和缺点? 41 172、Ajax和javascript的区别? 41 Servlet部分 42 174、JAVA SERVLET API中forward() 与redirect()的区别? 42 178、如何现实servlet...

    java面试宝典

    170、Javascript如何发送一个Ajax请求? 41 171、AJAX都有哪些有点和缺点? 41 172、Ajax和javascript的区别? 41 Servlet部分 42 174、JAVA SERVLET API中forward() 与redirect()的区别? 42 178、如何现实servlet...

    亮剑.NET深入体验与实战精要2

    本书既考虑到实际开发中经常遇到的困惑和难题,也分析了解决问题的思路和方法,更总结出项目开发中不可或缺的技术点及思想。读者可以在欣赏一个个有趣例子的过程中,不知不觉具备开发真正商业项目的能力。 本书集...

    亮剑.NET深入体验与实战精要3

    本书既考虑到实际开发中经常遇到的困惑和难题,也分析了解决问题的思路和方法,更总结出项目开发中不可或缺的技术点及思想。读者可以在欣赏一个个有趣例子的过程中,不知不觉具备开发真正商业项目的能力。 本书集...

    java web 视频、电子书、源码(李兴华老师出版)

    0314_AJAX开发技术 0400_第四部分:框架开发 0415_Struts基础开发 0416_Struts常用标签库 0417_Struts高级开发 0500_第五部分:附录 0518_附录A:实用工具 0519_附录B:MyEclipse开发工具 电子书目录: ...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 上

    16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>...

    MLDN+李兴华+Java+Web开发实战经典.part3.rar )

    16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、标签 16.4.2、与标签 16.4.3、标签 16.4.5、标签 16.4.6、标签 16.4.7、按钮标签 16.4.8、实例:编写基本表单 16.4.9、复选框标签 ...

    李兴华 java_web开发实战经典 源码 完整版收集共享

    16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 下

    16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>...

    李兴华Java Web开发实战经典.pdf (高清版) Part1

    16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<...

    李兴华 Java Web 开发实战经典 高清扫描版Part3

    16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<html:textarea>...

    李兴华Java Web开发实战经典(高清版) Part2

    16.3.5、重定向标签:<logic:redirect> 16.4、Html标签 16.4.1、<html:form>标签 16.4.2、<html:text>与<html:password>标签 16.4.3、<html:radio>标签 16.4.5、<...

    最新Java面试宝典pdf版

    52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念、线程的基本状态以及状态之间的关系 34 54、简述synchronized和java.util.concurrent.locks.Lock...

    Java面试笔试资料大全

    52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念、线程的基本状态以及状态之间的关系 34 54、简述synchronized和java.util.concurrent.locks.Lock...

    一个适合新手学习的电商项目

    // 跳转到登录页面,把用户请求的url作为参数传递给登录页面。 response.sendRedirect(userService.SSO_DOMAIN_BASE_USRL + userService.SSO_PAGE_LOGIN + "?redirect=" + request.getRequestURL()); // 返回...

    JAVA面试宝典2010

    52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念、线程的基本状态以及状态之间的关系 34 54、简述synchronized和java.util.concurrent.locks.Lock...

    Java面试宝典-经典

    52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念、线程的基本状态以及状态之间的关系 34 54、简述synchronized和java.util.concurrent.locks.Lock...

    Java面试宝典2010版

    52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念、线程的基本状态以及状态之间的关系 34 54、简述synchronized和java.util.concurrent.locks.Lock...

    java面试题大全(2012版)

    52、当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 33 53、线程的基本概念、线程的基本状态以及状态之间的关系 34 54、简述synchronized和java.util.concurrent.locks.Lock...

Global site tag (gtag.js) - Google Analytics