javascript - 当另一个 PHP 脚本运行时,AJAX 不会调用它的 PHP 脚本

标签 javascript php ajax html wamp

场景:

我有两个 PHP 脚本要同时调用:

  1. 第一个脚本将运行几分钟(基于 PHP 的文件下载),具体取决于下载的文件大小
  2. 第二个 PHP 脚本应该定期调用,以监视第一个脚本的执行 - 文件进度下载。为了避免在脚本完成时打开新窗口,它是通过 AJAX 调用的。

问题:

在第一个长时间运行的 PHP(稍后下载)脚本执行期间,不会处理定期调用的 AJAX 监控脚本。仅当第一个脚本完成时,AJAX 调用的 PHP 脚本才会被处理。

我花了很多时间来解决这个问题。我已经尽可能简化了我的测试脚本。但是,我仍然无法在执行主 php 脚本期间使 AJAX 脚本正常工作。我也无法以任何其他方式从主下载脚本获取中间反馈值。

请您分析一下我的代码示例好吗?它们具有我现在使用的精确形式。如果可能的话,您愿意在您的环境中运行它们吗?我怀疑问题可能出在我的 WAMP 环境中。

  • PHP 版本 5.4.12
  • Apache/2.4.4 (Win64) PHP/5.4.12
  • Windows 7 x64
  • 8GB 内存

代码示例:

调用两个 PHP 脚本的 JavaScript 代码:

<!DOCTYPE html>
<html>
<head>
    <title>Title of the document</title>
</head>

<body onload="callScripts();">


<script type="text/javascript">

    // call both PHP scripts(download and monitoring) in desired order
    callScripts = function()
    {
        // run long running (later Download) PHP script
        console.log("Calling: PHP/fileDownload.php");
        window.location.href = 'PHP/fileDownload.php';

        // call the monitoring PHP script multiple times in 2 second intervals
        window.setTimeout(function(){startDownloadMonitoring()}, 1000);
        window.setTimeout(function(){startDownloadMonitoring()}, 3000);
        window.setTimeout(function(){startDownloadMonitoring()}, 5000);
        window.setTimeout(function(){startDownloadMonitoring()}, 7000);
        window.setTimeout(function(){startDownloadMonitoring()}, 9000);
    };


    // call monitoring PHP script via AJAX
    function startDownloadMonitoring()
    {
        console.log("Calling startDownloadMonitoring()...");

        var xmlhttp;

        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                console.log("Response Received: " + xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET", "PHP/fileDownloadStatus.php", true);
        xmlhttp.send();
    }
</script>
</body>
</html>

PHP 监控脚本(fileDownloadStatus.php)

<?php

include 'ChromePhp.php';

// start session, update session variable, close session
session_start();
$_SESSION['DownloadProgress']++;
ChromePhp::log('$_SESSION[\'DownloadProgress\'] = ' . $_SESSION['DownloadProgress']);
session_write_close();    

echo "success";
?>

PHP 长时间运行脚本 (fileDownload.php)

<?php
include 'ChromePhp.php';

// disable script expiry
set_time_limit(0);

// start session, define variable, close session
session_start();

// prepare session variables
$_SESSION['DownloadProgress'] = 10;

session_write_close();

// execute for 60 seconds    
for( $count = 0; $count < 60; $count++)
{
    sleep(1);
}

?>

最佳答案

第一个脚本不是通过ajax发送的:

 // run long running (later Download) PHP script
  console.log("Calling: PHP/fileDownload.php");
  window.location.href = 'PHP/fileDownload.php';

您只需将用户重定向到另一个页面,并且由于 php 中有下载 header ,因此文件会在同一页面中下载。

您可以通过iframe轻松实现您的范围。您设置该 iframe 的源:'PHP/fileDownload.php',然后只需调用您的 ajax 下载检查器即可。

简短示例:

<iframe src="PHP/fileDownload.php">

<script>
        window.setTimeout(function(){startDownloadMonitoring()}, 1000);
        window.setTimeout(function(){startDownloadMonitoring()}, 3000);
        window.setTimeout(function(){startDownloadMonitoring()}, 5000);
        window.setTimeout(function(){startDownloadMonitoring()}, 7000);
        window.setTimeout(function(){startDownloadMonitoring()}, 9000);
        // .... blah blah
</script>

关于javascript - 当另一个 PHP 脚本运行时,AJAX 不会调用它的 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19683192/

相关文章:

jquery - Ruby on Rails 通过 ajax 搜索

javascript - 我如何仅使用数学算法在数组中找到最大值,特别是我每次都试图找出最小的数字

javascript - 使用 QUERY 或仅使用 CSS 的水平树/组织结构图/uml?

javascript - 在 z 轴上将图像夹在子对象和父对象之间

javascript - Ionic 项目,不在手机上工作,而是在开发过程中工作

javascript - Ajax 调用未从 Controller 返回成功

javascript - Firebase:使用预留主机 URL 时未创建 Firebase 应用程序 '[DEFAULT]'

php - 双重复 key 更新输入错误

php - 了解 PHP 和 MySQL 安全性基础知识

javascript - onclick 功能它仅在第二次单击后才起作用