jQuery 确定 $.ajax 错误是否是超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3543683/
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 15:37:40  来源:igfitidea点击:

Determine if $.ajax error is a timeout

jqueryajaxconnection-timeout

提问by coffeemonitor

I'm utilizing the magic of jQuery.ajax( settings ).

我正在利用jQuery.ajax( settings ).

However, I'm wondering if anyone has played with the timeout setting much?

但是,我想知道是否有人玩过超时设置?

I know it's basically for dictating the local time for a request, but can it trigger anything if the timeout is reached? Or does it simply stop listening for a response?

我知道它基本上是为了指定请求的本地时间,但是如果达到超时,它会触发任何事情吗?或者它只是停止监听响应?

Reading the jQuery site, I can see there are no arguments passed, so it seems like a simple setting with one capability. Which is fine.

阅读 jQuery 站点,我可以看到没有传递任何参数,所以这似乎是一个具有一种功能的简单设置。这很好。

But, I'd like to trigger an alert or some function if the timeout is reached. I can see that the error setting doesn't get triggered, in this case.

但是,如果达到超时,我想触发警报或某些功能。在这种情况下,我可以看到错误设置没有被触发。

Here's my snippet:

这是我的片段:

$("form#testform").submit(function(){ 

 var allFormValues = $("form#testform").serialize(); 

   $.ajax({
    cache:false,
    timeout:8000,  // I chose 8 secs for kicks
    type:"POST",
    url:"someurl.php",
    data:allFormValues,
    error:function(){ alert("some error occurred") },
    success:function(response){ alert(response); }
   });

});

Does anyone know how to work more with timeout?

有谁知道如何使用超时进行更多工作?

回答by David Hoerster

If your error event handler takes the three arguments (xmlhttprequest, textstatus, and message) when a timeout happens, the status arg will be 'timeout'.

如果您的错误事件处理程序在超时发生时采用三个参数(xmlhttprequest、textstatus 和 message),则状态参数将为“超时”。

Per the jQuery documentation:

根据jQuery 文档

Possible values for the second argument (besides null) are "timeout", "error", "notmodified" and "parsererror".

第二个参数(除了 null)的可能值是“超时”、“错误”、“未修改”和“解析器错误”。

You can handle your error accordingly then.

然后您可以相应地处理您的错误。

I created this fiddlethat demonstrates this.

我创建了这个小提琴来演示这一点。

$.ajax({
    url: "/ajax_json_echo/",
    type: "GET",
    dataType: "json",
    timeout: 1000,
    success: function(response) { alert(response); },
    error: function(xmlhttprequest, textstatus, message) {
        if(textstatus==="timeout") {
            alert("got timeout");
        } else {
            alert(textstatus);
        }
    }
});?

With jsFiddle, you can test ajax calls -- it will wait 2 seconds before responding. I put the timeout setting at 1 second, so it should error out and pass back a textstatus of 'timeout' to the error handler.

使用 jsFiddle,您可以测试 ajax 调用——它会在响应前等待 2 秒。我将超时设置设为 1 秒,因此它应该出错并将“超时”的文本状态传回错误处理程序。

Hope this helps!

希望这可以帮助!