是否可以在 jQuery.post() 上检查超时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4085605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:38:19  来源:igfitidea点击:

Is it possible to check timeout on jQuery.post()?

ajaxjquerytimeout

提问by horgen

I have this code requesting some folder info from a mysql DB:

我有这个代码从 mysql 数据库请求一些文件夹信息:

function gotoDir(pmcat_id, pcat_id){
    $('#slideshowContainer').html('<img class="loader" src="/javascript/ajax-loader.gif">');
    $.post("/publish/includes/content.includes/functions.slideshow.php", 
        {  updateSlideshowDir: 1, pmcat_id: pmcat_id, pcat_id: pcat_id },
        function(data){
             $('#pageSlideshow').html(data.content);
        }, "json"
    );
}

Sometimes the post request times out because of bad internet connection. Is it possible to set a timeout check on $.post() ? Ex: if $.post() uses more then X ms, reload the request.

有时,由于互联网连接不良,发布请求超时。是否可以对 $.post() 设置超时检查?例如:如果 $.post() 使用的时间超过 X 毫秒,则重新加载请求。

UPDATE: Looks like I found a solution:

更新:看起来我找到了一个解决方案:

function gotoDir(pmcat_id, pcat_id){
    $('#slideshowContainer').html('<img class="loader" src="/javascript/ajax-loader.gif">');
    $.ajax({
        type:"post",
        url:"/publish/includes/content.includes/functions.slideshow.php",
        data: { updateSlideshowDir: 1, pmcat_id: pmcat_id, pcat_id: pcat_id },
        dataType: "json",
        success:function(data) {
            if (data == null){
            alert('ajax failed. reloading...');
            gotoDir(pmcat_id, pcat_id);
        } else {
            $('#pageSlideshow').html(data.content);
        }
        }        
    });
}

Is this a OK way to do this? :S

这是一个好的方法吗?:S

回答by ifaour

$.ajax has all the functions you need to accomplish what you are asking for:

$.ajax 具有完成您所要求的所有功能:

function gotoDir(pmcat_id, pcat_id) {
    $('#slideshowContainer').html('<img class="loader" src="/javascript/ajax-loader.gif">');
    $.ajax({
        type: "POST",
        url: "/publish/includes/content.includes/functions.slideshow.php",
        data: { updateSlideshowDir: 1, pmcat_id: pmcat_id, pcat_id: pcat_id },
        dataType: "json",
        timeout: 500, // in milliseconds
        success: function(data) {
            // process data here
        },
        error: function(request, status, err) {
            if(status == "timeout") {
                gotoDir(pmcat_id, pcat_id);
            }
        }
    });
}

Please note that you don't need to set the timeoutoption unless you want to trigger the error method after a specific time youwant to set.

请注意,您不需要设置超时时间,除非你想有一个特定的时间后触发错误方法选择想要设置。

回答by MatuDuke

You should use $.ajax()and $.ajaxError(), then with "ajaxComplete" you can check if your request timedout, or succeded.

您应该使用$.ajax()and $.ajaxError(),然后使用“ajaxComplete”检查您的请求timedout, 或succeded.

Source: jQuery API

来源:jQuery API