javascript - 从 ajax 请求 (jQuery) 到 Code Igniter Controller 的 500 错误

标签 javascript php jquery ajax codeigniter-2

想知道有没有人遇到过这个问题。

我正在调用一个 CI Controller ,该 Controller 运行一个模型(在服务器上)并且针对特定场景需要更长的时间(大约 5 分钟)。问题是我在长时间请求后收到 500 错误,但是当请求较短(大约 1 分半钟)时我没有收到任何错误。

一些我已经检查过的东西:

  • CI 的“csrf_protection”关闭
  • 我在我的 ajax 调用中设置了很长的超时时间 (900000)
  • 我已将 PHP 中的 max_execution_time 设置为 900
  • 我已将 IIS 中的空闲超时设置为(20 分钟)

这是我的 ajax 调用:

$.ajax({
    type:'POST',
    url:"run/runScenario/" + saveOrUpdate + "/" + scenarioname + "/"  + units + "/" + stateid + "/" + climatestationid + "/" + soiltexture + "/" + 
                            moisturecontent + "/" + modsoilflag + "/" + slopelength + "/" +  slopeshape + "/" + slopesteepness + "/" + vegcommunity + "/" + 
                            basalcover + "/" + canopycover + "/" + rockcover + "/" + littercover + "/" + cryptogamscover,
    data: {scenarioDescription: scenariodescription},
    timeout:900000,
    statusCode: {
        500: function() {
          alert( "page not found" );
        }
      },
    success: function(runData){
            $('#progressBar').append('<p>Done running scenario.</p>');
            $('#progressBar').append('<p>Saving scenario...</p>');
            saveScenarioResultsTable();
            $('#progressBar').append('<p>Creating output table...</p>');
            printScenarioResultsTable(scenarioname);
    },
    error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.responseText);
                // stop timer
                var end = new Date().getTime();
                var execution_time = end - TIMER;
                runTimeMessage = "<br/><b>Ran scenario in " + (parseInt(execution_time) * 0.001) + " seconds.</b>";
                alert(runTimeMessage);
    }
});

更新 我创建了一个测试函数(作为我的运行 Controller 的一部分)并在该函数内设置了一个sleep(300)。我遇到了同样的 500 错误。
但是,当我更改为 sleep(299) 时,函数会成功运行。显然,每个请求有 5 分钟的限制。
我已经更改了 php.ini 中的 *max_execution_time*。还有其他建议吗?

UPDATE #2 我找到了问题的解决方案。问题是因为在我的 PHP 设置中没有启用“safe_mode”,PHP 超时在 CodeIgniter.php 中被覆盖(第 107 行)。我正在运行 CodeIgniter 2.1.4。我希望这可以帮助别人。 :)

最佳答案

只是一个想法,而不是代码的修复:

如果您将使用两个不同的 ajax 请求怎么办:

  1. 请求处理并将处理结果写入某些缓冲区,如文件或 sql 记录。此请求可能会或可能不会返回 future 资源的随机描述符。
  2. 请求按描述符或仅按资源可用性获取处理结果。此请求必须加入计时器。

时间线:

-------------------------------------------------------------------
Step         Local            server          processing
-------------------------------------------------------------------
1. Request for processing >>> Server >>> Thread for processing
2. Descriptor of resource <<< Server >>> Descriptor of resource
3.                            Server <<< Result of processing
4. Request by descriptor  >>> Server
5.                 Result <<< Server >>> Deletion of file or record
-------------------------------------------------------------------

那里:
1、2 - Ajax #1,按需触发。
4、5 - Ajax #2,每 10 秒触发一次。

关于javascript - 从 ajax 请求 (jQuery) 到 Code Igniter Controller 的 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19773603/

相关文章:

javascript - 如何获取 &lt;input type ="number"> 字段的原始值?

php - Symfony2 : Problems with function parameter

php - 在 php 中设置允许变量的松散翻译?

javascript - 视频模态 Bootstrap

javascript - Bootstrap Popovers(和一般的 Bootstrap )不工作

php - Ion Auth update_user() 不更新用户

jquery - Chrome 中的 JSON 中断按钮,IE 中的重新加载按钮(显示为裸数据)

javascript - 如何在 JQuery 中获取 .html() 内的值

jquery - 在MVC中添加动态表单内容

javascript - 圈复杂度路径计数