使用loginRequiredAjaxResponse解决AJAX会话超时 [ 未指定版本 ]
此方法要求Yii 1.1.9 或以上版本
问题
用户在较长时间内没有动作,php回话已经超时,回话过期后又有一个ajax请求但是程序并没有任何反应,例如在gridview中,gridview的内容完全消失。
正确的行为
显示登录页面,登录后再重定向回来。
解决方法
在配置文件中设置 loginRequiredAjaxResponse:
...
'components'=>array(
    'user'=>array(          
        ...
        'loginRequiredAjaxResponse' => 'YII_LOGIN_REQUIRED',
        ...
    ),
    ...
In your view or template
<?php
    if (Yii::app()->components['user']->loginRequiredAjaxResponse){
        Yii::app()->clientScript->registerScript('ajaxLoginRequired', '
            jQuery("body").ajaxComplete(
                function(event, request, options) {
                    if (request.responseText == "'.Yii::app()->components['user']->loginRequiredAjaxResponse.'") {
                        window.location.href = options.url;
                    }
                }
            );
        ');
    }
?>
注意:
以上代码在 options.url 为可用页面的url是测试通过,如果不是这种情况或者你只是想显示登录页面请替换:
window.location.href = options.url;
为
window.location.href = "'.Yii::app()->createUrl('/site/login').'";
英文原文: http://www.yiiframework.com/wiki/321/using-loginrequiredajaxresponse-to-solve-ajax-session-timeout/
张迪 北京
            注册时间:2011-09-05
最后登录:2019-05-23
在线时长:11小时30分
    最后登录:2019-05-23
在线时长:11小时30分
- 粉丝9
 - 金钱1210
 - 威望20
 - 积分1520
 
共 2 条评论
真假。。。
感觉还不错