javascript 超时在ajax发布请求中不起作用

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

Timeout not working in ajax post request

javascriptjqueryajaxhtml

提问by byJeevan

I am not able to make ajax error callback function of after 3 seconds. I tried with timeout, but it will not switch to error callback after specified time! I am not able to get the alert Got timeout.

我无法在 3 秒后制作 ajax 错误回调函数。我试过超时,但它不会在指定时间后切换到错误回调!我无法收到警报Got timeout

When I referred similar questions in this site with similar problems it didn't helped out. They all use ajax GET type. I am using jquery 1.10.1 library.

当我在这个网站上用类似的问题提到类似的问题时,它没有帮助。它们都使用ajax GET 类型。我正在使用 jquery 1.10.1 库。

script :

脚本 :

$.ajax({
  type: 'POST',
  timeout: 3000,
  url : "http://mydomain/Services.asmx/Best_Scores",
  dataType: "text",
  async:false,
  crossDomain:true,
  data: "strJsonRequest="+scoredata,
  success: function (data) {
    // Success code ...
  },
  error: function (data, textStatus, errorThrown) {
    if(textStatus == "timeout") {
      alert("Got timeout");
    }
  }
});

Any solution ?

任何解决方案?

回答by byJeevan

Fix :

使固定 :

Change async : falseto async: true

更改async : falseasync: true

Reason :

原因 :

A synchronous AJAX call blocks until the request has been finished. Implementing a timeout is not possible for technical reasons, because the AJAX call would have to be executed later.

同步 AJAX 调用会阻塞,直到请求完成。由于技术原因,无法实现超时,因为 AJAX 调用必须稍后执行。

If an AJAX call is executed later, the function somehow has to implement a blocking feature, to stop the code from running further after the AJAX call, and execute it again after a timeout - not possible.

如果稍后执行 AJAX 调用,该函数必须以某种方式实现阻塞功能,以在 AJAX 调用后停止进一步运行代码,并在超时后再次执行它 - 不可能。

回答by Mago Maverick

Today, (in 2019) I still have this problem.

今天,(在 2019 年)我仍然有这个问题。

I have an ajax call too long (depending from Date Start-> Date End) php script.

我有一个 ajax 调用太长(取决于日期开始-> 日期结束)php 脚本。

and after some minutes I get error 500, async: true don't help.

几分钟后,我收到错误 500,async: true 无济于事。

The call is:

电话是:

$.ajax({
    type: "GET",
    dataType: 'json',
    url: 'mymscript.php',
    contentType:'application/json;charset=UTF-8;',
    data: $('[name="myForm"]').serialize(),
    async:true,
    timeout:0,
success: function(response){
...

I resolved using: side PHP:

我解决了使用:side PHP:

set_time_limit(0);
ignore_user_abort(true);

at begin of script.

在脚本的开头。

echo ' ';
flush();
ob_flush();

in the middle of script (for example in main loop every day). This help to let client to don't disconnect (I think that is the main problem).

在脚本中间(例如每天在主循环中)。这有助于让客户端不要断开连接(我认为这是主要问题)。

Using this I continuosly write spaces befor the final json. Fortunately jquery trim spaces before to parse the json and, in the end, the json is valid.

使用这个我不断地为最终的 json 写空格。幸运的是 jquery 在解析 json 之前修剪了空格,最后,json 是有效的。

So I can catch response object to know if script is ended with errors or warnings.

所以我可以捕获响应对象以了解脚本是否以错误或警告结束。

I Hope this help somebody.

我希望这对某人有帮助。