我要做的是每2秒检查一次CodeIgniter,看看会话是否还活着。但由于某些原因,当我使用以下代码时,我收到此错误:
“Uncaught SyntaxError:Unexpected Token;”
有关为何发生这种情况的任何想法?
使用Javascript:
setInterval(function() {
$.getJSON("<?=base_url()?>index.php/regUserDash/sessionExpire"), function(data) {
var sessionState = jQuery.parseJSON('{"sessionExpired":"true","sessionExpired":"false"}');
if(sessionState.sessionExpired === "true") {
alert('expired');
} else if(sessionState.sessionExpired == "false") {
alert('not expired');
}
});
}, 2000);
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("sessionExpire" => true));
}
}
答案 0 :(得分:3)
这里有一个额外的括号:
$.getJSON("<?=base_url()?>index.php/regUserDash/sessionExpire") // <--
这应该有效:
setInterval(function() {
$.getJSON("<?=base_url()?>index.php/regUserDash/sessionExpire", function(data) {
var sessionState = $.parseJSON('{"sessionExpired":"true","sessionExpired":"false"}');
if (sessionState.sessionExpired === "true") {
alert('expired');
} else if (sessionState.sessionExpired == "false") {
alert('not expired');
}
});
}, 2000);