我與彗星試驗和我卡通過一個隱藏的iframe實現它(「永遠的框架」的IFrame彗星響應不包含任何數據
這是我的index.html:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
cometResponse = function() {
var debugOut = document.getElementById('debugOutput');
return function(response) {
debugOut.innerHTML = response.number;
}
}
</script>
</head>
<body>
<div id="debugOutput"></div>
<iframe src="comet.php"></iframe>
</body>
</html>
這是comet.php文件:
<?php
set_time_limit(0);
header('Content-Type: text/html');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Transfer-Encoding: chunked');
flush();
ob_flush();
$response = '<script type="text/javascript">
parent.cometResponse({
number: %1$d
});
</script>';
for ($i = 0; $i < 2; $i++) {
sleep(1);
$data = sprintf($response, $i);
$output = strtoupper(dechex(strlen($data)))."\r\n".$data."\r\n";
echo $output;
flush();
ob_flush();
}
echo "0\r\n\r\n";
加載頁面後,瀏覽器似乎「等待」響應幾秒鐘後,螢火顯示了這些響應頭空的響應:
HTTP/1.1 200 OK
Date: Mon, 26 Jul 2010 09:34:04 GMT
Server: Apache/2.2.14 (Win32) PHP/5.2.12
X-Powered-By: PHP/5.2.12
Cache-Control: no-cache, must-revalidate
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html;charset=ISO-8859-2
由於響應被視爲空,應該在響應中的標記也不會被執行。但是,如果我刪除了「Transfer-Encoding:chunked」標題,則正確地將內容發送到瀏覽器,但所有內容都在腳本末尾的一個大塊中,如預期的那樣。
我只是無法找到這裏出了什麼問題。
對此有何improvemtns? – 2011-08-11 01:28:57