【问题标题】:Ajax jQuery returning successfully after 10 seconds when it shouldn'tAjax jQuery 不应该在 10 秒后成功返回
【发布时间】:2011-07-09 00:41:57
【问题描述】:

我有一个 jQuery ajax 调用,它转到服务器 backend.php 脚本并被告知休眠 13 秒(或更长时间),然后用一个简单的测试消息进行响应。我看到的是 ajax 成功功能在 10 秒后触发(在萤火虫中观察到 200 响应),而服务器没有实际响应。如果我将响应延迟设置为小于 10 秒,我会完美地收到响应消息。

这是你以前见过的吗?是否发生了一些默认的 php 超时?

    ajaxcall = $.ajax({
    type: "GET",
    url: "backend.php",
    async: true,
    cache: false,
    dataType: 'json',
    timeout: 30000,
    /* Timeout in ms. Default to 30000 (30 seconds) */
    data: "TimeStamp=" + TimeStamp,
    success: function(data) {

        if (data !== null) {
            $('#TextHistory').append(data.message);

            if (data.timestamp != 0)
            {
              TimeStamp = data.timestamp;
            }
        }
        setTimeout(
        waitForMsg, /* Request next message */
        5000 /* ..after 5 seconds, default to 5000*/ );
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {

        $('#TextHistory').append('<p>Error:' + errorThrown + '</p>');

        setTimeout('waitForMsg()',"15000"); /* milliseconds (15seconds) default to 15000 */
    }
});

}

..在服务器端我只是这样做:

<?php

header('Content-Type: application/json');

sleep(13);   // Changing this to test it. Will only work if less than 10

   $When = date("H:i:s",strtotime("now"));

   $response["message"] = "<p>Small Test $When</p>";

    $response["timestamp"] = 123456789;

   echo json_encode($response);

?>

【问题讨论】:

    标签: php jquery ajax timeout response


    【解决方案1】:

    肯定会在 10 秒后超时

    在空白页上调用 phpinfo() 并查看超时设置为什么

    看看 max_execution_time 一个

    只需要在 php.ini 文件中或直接在 PHP 中覆盖它

    【讨论】:

    • @Dan Twining,通常关于时间的数字是指毫秒
    • 好的,好吧...我尝试设置“set_time_limit(35);”在我的后端脚本中并收到消息“警告:出于安全原因,set_time_limit()已被禁用......”我有一个streamline.net的共享主机帐户,所以我猜没有任何东西我能做到吗?
    • 我很好奇为什么它在 10 秒后在 php 信息中显示 30..hhhmmmm 时会超时。
    • 如果您的服务器不允许您睡眠超过 10 秒,我会在 Javascript 端使用 setTimeout()(或 jQuery 端使用 delay(),具体取决于您如何调用它) ) 用于 ajaxcall。
    猜你喜欢
    • 2013-12-31
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多