AJAX 请求 PHP 脚本循环
我四处寻找类似的主题,但似乎没有一个能解决我目前遇到的问题。 我有这个 JavaScript:
<script type="text/javascript">
http = new XMLHttpRequest();
function fetch()
{
http.open("GET", "script.php", true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}
function useHttpResponse()
{
if(http.readyState == 4)
{
var textout = http.responseText;
document.getElementById("ajax").innerHTML=textout;
}
}
</script>
非常基本的东西。 PHP 脚本是一个简单的循环:
for($i = 0; $i < 30000; $i++)
{
echo 'Hello<br />';
}
这非常有效。我按下一个带有 onClick="javascript:fetch()"
的按钮,它请求 PHP 脚本并在 ID 为“ajax”的 div 中输出 30.000 行“Hello”。
问题是它必须等到运行完所有 30.000 个循环。我希望它在每个循环之后通过 AJAX 请求输出响应,以便列表在脚本运行时扩展。这可能吗?我该怎么做?就像我说的,我已经搜索过,但一无所获。我意识到这严格来说只是一种美容效果,但如果您有任何建议,我将不胜感激!
谢谢
I have looked around for similar topics but none seem to address the problem I am currently having.
I have this JavaScript:
<script type="text/javascript">
http = new XMLHttpRequest();
function fetch()
{
http.open("GET", "script.php", true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}
function useHttpResponse()
{
if(http.readyState == 4)
{
var textout = http.responseText;
document.getElementById("ajax").innerHTML=textout;
}
}
</script>
Extremely basic stuff. PHP-script is a simple loop:
for($i = 0; $i < 30000; $i++)
{
echo 'Hello<br />';
}
This works great. I press a button that has onClick="javascript:fetch()"
and it requests the PHP-script and outputs 30.000 lines of "Hello" in the div with id "ajax".
The problem is that it has to wait until it has run all 30.000 loops. I want it to output the response via the AJAX request after EACH loop so that the list kind of expands as the script is running. Is this possible? How would I do this? Like I said, I have searched but come up empty. I realize this is strictly a cosmetic effect but I would be greatful for any advices!
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 PHP 设置中禁用输出缓冲或调用 http://www.php.net/每次迭代后,manual/en/function.flush.php。
正如迈克所指出的。在 XHR 的
onreadystatechange
中,您正在检查status == 4
,这意味着所有数据都已传输。您应该检查status == 3
,这意味着请求正在处理中;通常,响应中提供了一些部分数据,但服务器尚未完成其响应。
Mike 还指出,如果您的服务器设置为使用压缩,则不得缓冲所有输出,并且您无法流式传输您的 HTML 内容。
disable output buffering in your PHP settings or call http://www.php.net/manual/en/function.flush.php after each iteration.
As pointed out by Mike. in your XHR's
onreadystatechange
, you're checking forstatus == 4
, which means all the data has been transferred. You should check forstatus == 3
, which meansThe request is in process; often some partial data is available from the response, but the server isn't finished with its response.
Mike also pointed out that if your server is set to use compression, then all output must not be buffered and you cannot stream your HTML content.
不,这是不可能的。即使已发送一些输出,您也无法在脚本运行时获取数据。 AJAX只能读取完整的结果。
请参阅我就此主题提出的问题:长轮询/HTTP 流一般问题
No, it's not possible. You cannot fetch data while the script is running, even if some output has been sent. AJAX can only read the complete result.
See the question I asked on this subject: Long Polling/HTTP Streaming General Questions