繁体   English   中英

Yii CGridView捕获Ajax响应并解决会话超时

[英]Yii CGridView catch Ajax Response and solve session timeout

我正在使用Yii MVC ;

用户已登录;

我正在使用带有2个标签的浏览器;

从第二个选项卡中,我注销了用户,因此,现在,用户也从第一个选项卡中退出了。

现在,我正在尝试在gridview使用ajax call查询db

用户已注销,因此,我得到了302状态代码;

这是我要捕获此错误的位置,并将用户重定向到登录页面,因为会话已过期。

在内部,Yii尝试将我重定向到login页面,但失败;

我看到带有200状态代码的login操作,但是没有重定向。

我找到了有关此行为的一些信息,但它不起作用:

使用loginRequiredAjaxResponse解决Ajax会话超时

我试图使用一个新的yii项目,因为我怀疑应该归咎于user module

它不是用户模块,而是yii。

在框架中,在CAccessControlFilter ,调用以下函数;

如果会话已过期并且刷新页面,则可以看到死消息;

但是,如果会话已过期并且我有一个ajax调用,则不会发生重定向。

protected function accessDenied($user,$message)
    {
        die(__FUNCTION__);
        debug_print_backtrace();
        if($user->getIsGuest())
            $user->loginRequired();
        else
            throw new CHttpException(403,$message);
    }

main.php文件中的组件->用户下,我设置'loginRequiredAjaxResponse' => 'YII_LOGIN_REQUIRED',

http://www.yiiframework.com/wiki/321/using-loginrequiredajaxresponse-to-solve-ajax-session-timeout/

在模板或视图中放入以下代码:

<?php
        /*
         * if ajax call and session has expired, then redirect to user/login
         */
        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 = "'.Yii::app()->createUrl('/user/login').'";
                    }
                }
            );
        ');
        }
        ?>

在自定义成员控制器中,将其放在init方法中

    if (Yii::app()->user->isGuest) {
        /*
         *  covers even the ajax requests if user session has expired
         */
        Yii::app()->user->loginRequired();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM