javascript - jQuery AJAX 在 Laravel 5.2 中使用 jQuery 空闲超时中止

标签 javascript php jquery ajax laravel-5

我目前正在使用jQuery Idle Timeout在我的 Laravel 5.2 系统中。
它在我的本地计算机(MAMP Pro)中工作正常,但是当我将其上传到开发服务器时,AJAX get 请求给出“中止”错误: jQuery AJAX request Aborted error in Laravel 5.2

问题出在哪里?

Laravel 路由代码:

Route::get('/keepalive', function () {
    return view('keepalive');
});

jQuery 代码:

$(function() {
// dialog is in the main layout after login = admin.blade.php
$("#dialog_session_timeout").dialog({
    autoOpen: false,
    modal: true,
    width: 400,
    height: 200,
    closeOnEscape: false,
    draggable: false,
    resizable: false,
    buttons: {
        'Yes, Keep Working': function() {
            $(this).dialog('close');
        },
        'No, Logoff': function() {
            // fire whatever the configured onTimeout callback is.
            // using .call(this) keeps the default behavior of "this" being the warning
            // element (the dialog in this case) inside the callback.
            $.idleTimeout.options.onTimeout.call(this);
        }
    }
});
// cache a reference to the countdown element so we don't have to query the DOM for it on each ping.
var $countdown = $("#dialog-countdown");
var _token = $('meta[name="csrf-token"]').attr('content');
var data = {
    '_token': _token
};
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

// start the idle timer plugin
$.idleTimeout('#dialog_session_timeout', 'div.ui-dialog-buttonpane button:first', {
    //idleAfter: 5, // in seconds
    idleAfter: 7170, // 2 hours minus 30 seconds. 2 hours in the config/sessions.php
    pollingInterval: 10,
    //keepAliveURL: base_url + '/keepalive.php',
    keepAliveURL: base_url + '/keepalive',
    data: data,
    serverResponseEquals: 'OK',
    onTimeout: function() {
        window.location = "logout";
    },
    onIdle: function() {
        $(this).dialog("open");
    },
    onCountdown: function(counter) {
        $countdown.html(counter); // update the counter
    }
});

最佳答案

我也遇到过这种类型的问题,它在本地工作正常,但在生产服务器上却不行。 由于 Ajax 选项而引发错误

timeout

jquery-idletimeout 插件中的默认 AJAXTimeout 选项为 250(毫秒),有时对于请求来说较低。您需要在idletimeout初始化中显式定义“AJAXTimeout”选项,例如:

// start the idle timer plugin
$.idleTimeout('#dialog_session_timeout', 'div.ui-dialog-buttonpane button:first', {
    idleAfter: 7170, // 2 hours minus 30 seconds. 2 hours in the config/sessions.php
    AJAXTimeout: 2000,  // instead of 250 ms
    pollingInterval: 10,
    keepAliveURL: base_url + '/keepalive',
    //data: data,      // no option like this exist in idletimeout plugin
    serverResponseEquals: 'OK',
    onTimeout: function() {
       window.location = "logout";
    },
    onIdle: function() {
       $(this).dialog("open");
    },
    onCountdown: function(counter) {
       $countdown.html(counter); // update the counter
    }
});

或者转到“jquery.idletimeout.js”文件并注释ajax请求中的超时选项,如下所示:

$.ajax({
    //timeout: options.AJAXTimeout,
    url: options.keepAliveURL,
    error: function(){
        self.failedRequests--;
    },
    success: function(response){
        if($.trim(response) !== options.serverResponseEquals){
            self.failedRequests--;
        }
    },
    complete: function(){
        if( recurse ){
            self._startTimer();
        }
    }
});

注意:您不能在idletimeout选项中设置“data”属性,而是可以在jquery.idletimeout.js文件中存在的ajax请求中设置它,但实际上不需要设置该选项。 :)

关于javascript - jQuery AJAX 在 Laravel 5.2 中使用 jQuery 空闲超时中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35143230/

相关文章:

php - Page.php 不显示 wordpress 中的更改

php - 如何制作一个仅显示 MySQL 中最后 20 个值的 php 动态表?

javascript - 如何自动选择下拉列表

java - 如何从 phonegap 插件获取 android 服务返回消息

php - 如何使用 MySQL PHP 以正确的方式执行多级菜单?

javascript - 动画正在降低网页的性能

javascript - ie javascript 删除元素

javascript - eval 不计算数字前是否为零

javascript - Node js从函数返回值

javascript - HTML5 : Canvas not drawing anything