考虑写入 div 的 AJAX 调用:
recent_req=$.post('result.php', { d: data }, function(returnData) {
$('#content').html(returnData);
});
PHP 脚本位于result.php
执行一些需要时间的功能,每步大约 5-20 秒。我正在使用 PHPflush()
函数可以在每个步骤开始和结束时立即将信息发送到浏览器,但是如何让 Javascript 将数据写入#content
div 进来时?
Thanks.
EDIT:澄清一下:假设result.php
如下所示,由于限制,实际上无法重构:
<?php
echo "Starting...<br />";
flush();
longOperation();
echo "Done with first long operation.<br />";
flush();
anotherLongOperation();
echo "Done with another long operation.<br />";
flush();
?>
AJAX 的结构如何来调用result.php
这样 echo 语句就会附加到#content
div 当他们进来时?欢迎任何带/不带 jQuery 的解决方案。谢谢!
您可以使用一种使用 iframe 的技术来实现此目的。
与涉及框架的其他建议类似,但它不涉及会话或轮询或任何其他内容,并且不需要您显示 iframe 本身。它还具有在流程中的任何时刻运行您想要的任何代码的好处,以防您在 UI 上做一些比仅仅将文本推送到 div 更复杂的事情(例如,您可以更新进度条)。
基本上,将表单提交到隐藏的 iFrame,然后将 javascript 刷新到该框架,该框架与 iFrame 父级中的函数进行交互。
像这样:
HTML:
<form target="results" action="result.php" method="post">
<!-- your form -->
<input type="submit" value="Go" />
</form>
<iframe name="results" id="results" width="0" height="0" />
<div id="progress"></div>
Javascript,在您的主页中:
function updateProgress(progress) {
$("#progress").append("<div>" + progress + "</div>");
}
结果.php:
<?php
echo "<script language='javascript'>parent.updateProgress('Starting...');</script>";
flush();
longOperation();
echo "<script language='javascript'>parent.updateProgress('Done with first long operation.');</script>";
flush();
anotherLongOperation();
echo "<script language='javascript'>parent.updateProgress('Done with another long operation.');</script>";
flush();
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)