【发布时间】:2016-06-23 06:28:52
【问题描述】:
在我的 Laravel 应用程序中,我有一个通知系统。这是使用 AJAX 轮询技术(即每 5 秒左右运行一次 ajax post 函数)运行的,类似于:
$.ajaxSetup({
headers: {
'X-CSRF-Token': CSRF_TOKEN
}
});
(function pollForNewNotifications() {
setTimeout(function () {
$.ajax({
type: 'POST',
url: 'http://example.com/get-notifications',
dataType: 'json',
data: {
// data that is sent
},
success: function (data) {
// add new notifications if data is not empty logic
pollForNewNotifications();
}
})
.fail(function (xhr, status, err) {
console.error(xhr.responseText)
});
}, 5000);
})();
现在大部分时间都有效。
但是,问题是我有时会发现,如果长时间不活动,我会收到 TokenMismatchException in VerifyCsrfToken.php 错误并自动注销。
我认为是这种情况是因为 CSRF 令牌更改或更长是有效的(我可能错了)。
我该如何解决这个问题?
【问题讨论】:
标签: ajax laravel laravel-5 csrf csrf-protection