XMLHttpRequest在PHP脚本运行时获取数据
I am trying to poll a PHP page for an updated $val variable value, but for some reason it doesn't want to show anything on the calling html page
PHP (generator.php) script:
<?php
header('Content-type: text/html; charset=utf-8');
function output($val)
{
echo $val;
flush();
ob_flush();
usleep(500000);
}
output('Begin... (counting to 10)');
for( $i = 0 ; $i < 10 ; $i++ )
{
output($i+1);
}
output('End...');
?>
HTML page:
<html>
<head>
<script type="text/javascript">
function generate(){
try {
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
}
xmlhttp.onreadystatechange = refreshwait;
xmlhttp.open('GET', './generator.php', true);
xmlhttp.send('null');
}
function refreshwait() {
if (xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
var code = 'Finished';
document.getElementById('view_area').innerHTML = code;
}
}else{
document.getElementById("view_area").innerHTML = xmlhttp.responseText;
refreshwait();
}
}
</script>
</head>
<body>
<div id="a_form">
<form id="aform">
<input name="Generate" onClick="generate()" type="button" value="Generate Fresh File" />
</form>
</div>
<div id="view_area"></div>
</body>
</html>
What am I doing wrong? I am expecting to see a count from 1 to 10 appear on the html page as the php is running.
我正在尝试轮询PHP页面以获取更新的$ val变量值,但由于某种原因它不会 想要在调用html页面上显示任何内容 p>
PHP(generator.php)脚本: p>
&lt;?php
header ('Content-type:text / html; charset = utf-8');
function output($ val)
{
echo $ val;
flush();
ob_flush(); \ n usleep(500000);
}
output('Begin ...(count to 10)');
for($ i = 0; $ i&lt; 10; $ i ++)
{
output($ i + 1);
}
output('结束...');
?&gt;
code> pre>
HTML页面: p>
&lt; html&gt;
&lt; head&gt;
&lt; script type =“text / javascript”&gt;
function generate(){
try {
xmlhttp = window.XMLHttpRequest? new XMLHttpRequest():new ActiveXObject(“Microsoft.XMLHTTP”);
} catch(e){
}
xmlhttp.onreadystatechange = refreshwait;
xmlhttp.open('GET','。/ generator) .php',true);
xmlhttp.send('null');
}
函数refreshwait(){
if if(xmlhttp.readyState == 4){
if (xmlhttp.status == 200){
var code ='Finished';
document.getElementById('view_area')。innerHTML = code;
}
} else {
document.getElementById(“view_area”)。innerHTML = xmlhttp.responseText;
refreshwait();
}
}
&lt; / script&gt;
&lt; / head&gt;
&lt; body&gt;
&lt; div id =“a_form”&gt;
&lt; form id = “aform”&gt;
&lt;输入名称=“生成”onClick =“generate()”type =“button”value =“生成新文件”/&gt;
&lt; / form&gt;
&lt; / div&gt;
&lt; ; div id =“view_area”&gt;&lt; / div&gt;
&lt; / body&gt;
&lt; / html&gt;
code> pre>
我做错了什么? 因为php正在运行,我希望看到html页面上出现1到10的计数。 p>
div>
I can think of four potential issues:
Some servers (SAPIs) feature internal output buffering, typically to a size of 4Kb. This is because sending "chunks" of this size is generally more efficient than sending many smaller pieces.
Some browsers will read chunks of a certain size (again, typically 4Kb) before rendering them to the document. This again improves performance by not re-rendering the page every time a few bytes happen to show up in the network stream.
Some browsers (particularly older IE, can't remember which version) do not allow access to
responseText
until thereadyState
is4
. Obviously this is a limitation, and it has been removed in newer browsers, but it's still a possible problem.EDIT: Some browsers require you to define
onreadystatechange
after calling.open()
. This is to allow reuse ofXMLHttpRequest
objects. Calling.open
effectively "resets" the object.
Possible solution: Assuming the problem is 1 or 2, rather than 3 or 4, then you can try outputting echo $val.str_repeat(" ",5000);
to try and circumvent the output buffering. Obviously this will bloat your bandwidth, but the result should look the same (because spaces are collapsed in HTML)