我需要PHP流输出到Javascript支持,但是使用Javascript保留旧回应并打印它们是这样的...如何正确刷新PHP输出缓冲区?
控制台日志:
[0]: Line to show.
[0]: Line to show.[1]: Line to show.
[0]: Line to show.[1]: Line to show.[2]: Line to show.
[0]: Line to show.[1]: Line to show.[2]: Line to show.[3]: Line to show.
[0]: Line to show.[1]: Line to show.[2]: Line to show.[3]: Line to show.[4]: Line to show.
[0]: Line to show.[1]: Line to show.[2]: Line to show.[3]: Line to show.[4]: Line to show.Array
(
[0] => [0]: Line to show.
[1] =>
[2] =>
[3] => [1]: Line to show.
[4] =>
[5] =>
[6] => [2]: Line to show.
[7] =>
[8] =>
[9] => [3]: Line to show.
[10] =>
[11] =>
[12] => [4]: Line to show.
[13] =>
[14] =>
)
所以使用Javascript控制台日志状态的responseText的是“节能”旧的回应。然而,看看我保存在PHP中的数组,你可以看到没有以前的回声被刷新到JS。
的Javascript:
$.ajax({
url: "../controller/controller.php",
type: "POST",
data: {operation: 'rxMode'},
xhr: function(){
var xhr = $.ajaxSettings.xhr();
xhr.onprogress = function(e){ console.log(e.currentTarget.responseText); };
console.log(xhr);
return xhr;
}
});
PHP:
$out = array();
for ($i = 0; $i<5; $i++){
echo "[$i]: Line to show.";
array_push($out, ob_get_contents());
ob_flush();
array_push($out, ob_get_contents());
flush();
array_push($out, ob_get_contents());
sleep(2);
}
print_r($out);
我的期望responseText的是
[0]: Line to show.
[1]: Line to show.
[2]: Line to show.
[3]: Line to show.
[4]: Line to show.
编辑:我不想删除旧的答复,而我宁愿使用Javascript只给我我想要的responseText。
移除PHP数组和print_r并不能解决问题,它仅用于调试。 –
也许尝试在下一个响应之前清除xhr var?与'remove'命令一样。 **但是**您将不得不从xhr声明中移除'var'。 – Soaku
不清楚你的意思是通过删除变量。它在jQuery ajax包装中,所以删除它会导致ajax指向错误的变量? –