Javascript 当另一个PHP脚本正在运行时,AJAX不会调用它的PHP脚本 脚本:
我需要同时调用两个PHP脚本:Javascript 当另一个PHP脚本正在运行时,AJAX不会调用它的PHP脚本 脚本:,javascript,php,ajax,html,wamp,Javascript,Php,Ajax,Html,Wamp,我需要同时调用两个PHP脚本: 第一个脚本将运行几分钟(基于PHP的文件下载),具体取决于下载的文件大小 第二个PHP脚本应该定期调用,以监视第一个脚本文件下载进度的执行。为了避免在脚本完成时打开新窗口,通过AJAX调用它 问题: 在执行第一个长时间运行的PHP(稍后下载)脚本期间,不会处理通常称为AJAX监视脚本的脚本。只有当第一个脚本完成时,称为PHP脚本的AJAX才会得到处理 我花了很多时间研究这个问题。我已经尽可能地简化了我的测试脚本。但是,在执行主php脚本期间,我仍然无法使AJAX脚
- PHP版本5.4.12
- Apache/2.4.4(Win64)PHP/5.4.12
- Windows 7 x64
- 8GB内存
文件标题
//按所需顺序调用两个PHP脚本(下载和监视)
callScripts=function()
{
//运行长时间运行(稍后下载)PHP脚本
log(“调用:PHP/fileDownload.PHP”);
window.location.href='PHP/fileDownload.PHP';
//在2秒钟的时间间隔内多次调用监视PHP脚本
setTimeout(函数(){startDownloadMonitoring()},1000);
setTimeout(函数(){startDownloadMonitoring()},3000);
setTimeout(函数(){startDownloadMonitoring()},5000);
setTimeout(函数(){startDownloadMonitoring()},7000);
setTimeout(函数(){startDownloadMonitoring()},9000);
};
//通过AJAX调用监控PHP脚本
函数startDownloadMonitoring()
{
log(“调用startDownloadMonitoring()…”;
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
log(“收到的响应:+xmlhttp.responseText”);
}
}
open(“GET”,“PHP/fileDownloadStatus.PHP”,true);
xmlhttp.send();
}
PHP监控脚本(fileDownloadStatus.PHP)
PHP长时间运行脚本(fileDownload.PHP)
它不是通过ajax发送的第一个脚本:
// run long running (later Download) PHP script
console.log("Calling: PHP/fileDownload.php");
window.location.href = 'PHP/fileDownload.php';
您只需将用户重定向到另一个页面,因为php中有download headers
,所以文件将在同一页面中下载
您可以通过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>
setTimeout(函数(){startDownloadMonitoring()},1000);
setTimeout(函数(){startDownloadMonitoring()},3000);
setTimeout(函数(){startDownloadMonitoring()},5000);
setTimeout(函数(){startDownloadMonitoring()},7000);
setTimeout(函数(){startDownloadMonitoring()},9000);
// .... 废话
它不是通过ajax发送的第一个脚本:
// run long running (later Download) PHP script
console.log("Calling: PHP/fileDownload.php");
window.location.href = 'PHP/fileDownload.php';
您只需将用户重定向到另一个页面,因为php中有download headers
,所以文件将在同一页面中下载
您可以通过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>
setTimeout(函数(){startDownloadMonitoring()},1000);
setTimeout(函数(){startDownloadMonitoring()},3000);
setTimeout(函数(){startDownloadMonitoring()},5000);
setTimeout(函数(){startDownloadMonitoring()},7000);
setTimeout(函数(){startDownloadMonitoring()},9000);
// .... 废话
当你打电话时
window.location.href = 'PHP/fileDownload.php';
脚本执行将停止(不是立即停止,请参阅)
是否确实执行了对fileDownloadStatus.php的ajax调用
解决方案是通过ajax异步调用调用fileDownloadStatus.php文件,或者将其加载到页面中的iframe中
希望有帮助。当你打电话时
window.location.href = 'PHP/fileDownload.php';
脚本执行将停止(不是立即停止,请参阅)
是否确实执行了对fileDownloadStatus.php的ajax调用
解决方案是通过ajax异步调用调用fileDownloadStatus.php文件,或者将其加载到页面中的iframe中
希望有帮助。首先,检查您的浏览器是否启动了这些AJAX请求。@CBroe,您好,谢谢您的回复。我确信它开始了。我做了很多实验,可能太多了。我做的一个实验是,首先让AJAX调用运行,几秒钟后,我启动了长时间运行的php DowlLoad脚本。下载php脚本启动时,AJAX脚本停止工作。您是否通过浏览器调试工具的网络面板验证AJAX请求正在启动?老实说,没有。我没有那样检查。我用Chrome来开发。你能给我一个简短的建议吗,