0

我对以下代码做错了吗?当 Ci 会话到期时,我似乎无法让 alert("success") 进行处理。下面是我的 jquery 和 Ci 代码:

setInterval(function() {
    jQuery.getJSON("<?=base_url()?>index.php/regUserDash/sessionExpire", function(data) {
        var sessionState = jQuery.parseJSON('{"sessionExpired":"true","sessionExpired":"false"}');
        if(sessionState.sessionExpired === "true") { // if session is expired run the following code
            var dataString = 'true';
            jQuery.ajax({   // send the expired signal to Ci so that it knows the session has expired
                type: 'POST',
                dataType: 'JSON',
                url: '<?=base_url()?>index.php/regUserDash/extendSession',
                data: {'dataString': true},
                success: function(data) {
                    if (data.extendedSession == true) {
                        alert('success');
                    } else {
                        return false;
                    }
                }
            });
        } else if(sessionState.sessionExpired == "false") {
            return;
        }
    });
}, 120000); // loop through every 2 minutes

CodeIgniter 代码:

public function sessionExpire() {
        if ($this->session->userdata("logged") == "1") {
            echo json_encode(array("sessionExpired" => false));
        } elseif($this->session->userdata("logged") == "0") {
            echo json_encode(array("sessionExpire" => true));
        }
    } public function extendSession() {
        // set loggedIn session var
        $this->session->set_userdata('logged', '1');
        // return json to ajax call
        echo json_encode(array("extendedSession" => true));
    }
4

1 回答 1

2

jQuery.parseJSON('{"sessionExpired":"true","sessionExpired":"false"}');返回 firstsessionExpired = true和 second sessionExpired = false,覆盖第一个。

因此,这是执行此操作的一种方法(需要测试):

setInterval(function() {
    jQuery.getJSON("<?=base_url()?>index.php/regUserDash/sessionExpire", function(data) {
        if(data.sessionExpired == "true") { // if session is expired run the following code
            // var dataString = 'true'; // Remove this
            jQuery.ajax({   // send the expired signal to Ci so that it knows the session has expired
                type: 'POST',
                dataType: 'JSON',
                url: '<?=base_url()?>index.php/regUserDash/extendSession',
                data: {'dataString': true},
                success: function(data) {
                    //if (data.extendedSession == true) {
                        alert('success');
                    //} else {
                    //    return false;
                    //}
                    // Remove commented code, because extendSession() always return true
                }
            });
        } else {
            return 'Session not expired!';
        }
    });
}, 120000); // loop through every 2 minutes

和 CI 功能:

public function sessionExpire()
{
    if ($this->session->userdata("logged") == "1") {
        echo json_encode(array("sessionExpired" => false));
    } elseif ($this->session->userdata("logged") == "0") {
        echo json_encode(array("sessionExpired" => true));
    }
}

public function extendSession()
{
    // set loggedIn session var
    $this->session->set_userdata('logged', '1');
    // return json to ajax call
    echo json_encode(array("extendedSession" => true));
}
于 2012-07-28T22:58:40.837 回答