【问题标题】:JQuery .ajax timeout increase not workingJQuery .ajax 超时增加不起作用
【发布时间】:2013-08-28 01:12:29
【问题描述】:

我有一个网页,其中 javascript 通过 .ajax() 函数使用 POST 请求调用 PHP 服务器。 PHP 服务器依次调用外部第三方 API 来提交文本分析作业。处理完文本分析作业后,我的 PHP 服务器会从 API 查询结果。但是,第三方 REST API 不提供任何查询作业状态的方法。因此,在我提交作业后,我让程序休眠大约一分钟,然后查询结果。但是,有时我的结果是不完整的。我尝试将睡眠时间设置得很大,但将其设置为超过一分钟似乎会使从 Javascript 到 PHP 的初始 POST 请求超时。将 ajax 超时参数设置为高无济于事。有人对如何解决这个问题有建议吗?非常感谢任何帮助。

ajax 请求如下所示:

function callServer(params, success) {
    var url = "ie.php";
    $.ajax({
       type: 'POST',
       url: url,
       data: params,
       timeout: 60000000, //time out parameter doesn't work
       dataType: "json",
       success: function(result, textStatus) {
                    console.log(JSON.stringify(result, null, '  '));
            if (success) success(result);
       },
       error: function(xhr, textStatus, errorThrown) {
                    var text = 'Ajax Request Error: ' + 'XMLHTTPRequestObject status: ('+xhr.status + ', ' + xhr.statusText+'), ' +
                                            'text status: ('+textStatus+'), error thrown: ('+errorThrown+')';
                    console.log('The AJAX request failed with the error: ' + text);
                    console.log(xhr.responseText);
                    console.log(xhr.getAllResponseHeaders());
       }
    });
}

错误如下所示:

AJAX 请求失败,错误为:Ajax 请求错误:XMLHTTPRequestObject 状态:(0,错误),文本状态:(错误),抛出错误:()

【问题讨论】:

  • 为什么您认为这是由于您的超时不起作用造成的?听起来 ie.php 是返回错误的那个。你的错误日志是怎么说的?
  • 如果我将等待时间减少到 60 秒以下,一切正常。我尝试更改 php.ini 以使 max_execution_time 更长,但这也无济于事。
  • 网络服务器错误日志在ie.php 的主机上显示了什么?
  • 如果你在浏览器中运行ie.php,会出现什么错误?
  • 服务器日志没有错误..

标签: php javascript ajax jquery


【解决方案1】:

整个过程多处有超时设置:

  1. 浏览器/Ajax 调用
  2. PHP 有自己的:ini_set('max_execution_time', '1800');set_time_limit(1800);
  3. Apache 本身有一个最大运行时间:超时apache2.conf
  4. 可能是您的 REST API - 无论您如何称呼它

我认为最好的情况是从阻塞/同步策略变为异步策略:

  1. 使用 ajax 将请求从客户端发送到您的服务器
  2. 从您的服务器异步运行 REST API(查找异步 curl 或 node.js)
  3. 客户端使用 ajax 定期查询服务器的状态(可能每 10 秒)

这使得整个过程异步,并且在处理发生时不需要您等待或阻塞

【讨论】:

  • 我想添加另一个发生这种情况的地方是 IIS 本身。如果您通过 FastCGI 运行 IIS 和 PHP,请进入 FastCGI 设置并使用空闲超时、请求超时和活动超时(对我来说是什么)设置。
【解决方案2】:

你能做到吗?

function callServer(params, success) {
    var url = "ie.php";
    setTimeout(function(){
        $.ajax({
           type: 'POST',
           url: url,
           data: params,
           dataType: "json",
           success: function(result, textStatus) {
                        console.log(JSON.stringify(result, null, '  '));
                if (success) success(result);
           },
           error: function(xhr, textStatus, errorThrown) {
                        var text = 'Ajax Request Error: ' + 'XMLHTTPRequestObject status: ('+xhr.status + ', ' + xhr.statusText+'), ' +
                                                'text status: ('+textStatus+'), error thrown: ('+errorThrown+')';
                        console.log('The AJAX request failed with the error: ' + text);
                        console.log(xhr.responseText);
                        console.log(xhr.getAllResponseHeaders());
           }
        })
    }, 200);
}

【讨论】:

    【解决方案3】:

    要修复此类超时错误,您需要在 3 或 4 个不同的位置设置超时长度:

    1. Jquery 本身(可以像您一样使用 timeout 参数完成,也可以使用 $.ajaxSetup 函数全局完成)

    2. PHP 通过在 php.ini 中设置 max_execution_time

    3. 您的 Web 服务器(Apache 和 IIS 不同。例如,在 IIS 中,如果您通过 FastCGI 运行 PHP,则必须在 FastCGI 设置中设置空闲超时、请求超时和活动超时)

    4. 可能是您的浏览器本身,尽管过去这对我来说不是必需的

    如果您在所有这些地方都设置了超时,它应该可以工作。对我来说,我已经像你一样完成了所有其他步骤,但直到我在我的 FastCGI 设置中设置了活动超时设置后问题才得到解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 2012-08-07
      • 2014-09-11
      • 2015-09-25
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多