2010-07-26 25 views
0

我與彗星試驗和我卡通過一個隱藏的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」標題,則正確地將內容發送到瀏覽器,但所有內容都在腳本末尾的一個大塊中,如預期的那樣。

我只是無法找到這裏出了什麼問題。

+0

對此有何improvemtns? – 2011-08-11 01:28:57

回答

0

兩個猜測:

  1. 內容編碼:gzip

    也許mod_gzip的工作不正常? 你有沒有試過別的主機?

  2. 也許火狐忽略的代碼,如果 它不是內< HTML>
+0

1.也許,我會研究一下。 2.不,螢火蟲通常顯示迴應的原始內容。 – selfawaresoup 2010-08-02 06:52:39

0

這可能幫助別人,這是我如何解決它:

<?php 
header('Content-Encoding: chunked'); 
header('Transfer-Encoding: chunked'); 
header('Content-Type: text/html'); 
header('Connection: keep-alive'); 
header('Cache-Control: no-cache, must-revalidate'); 
flush(); 
set_time_limit(0); 
function chunk($data) { 
    echo sprintf("%x\r\n%s\r\n", strlen($data), $data); 
    flush(); 
    ob_flush(); 
} 
// Code to output data here. 
// The following loop is an example. 
for($i = 0; $i < 5; $i++) { 
    chunk('<script type="text/javascript">window.top.test();</script>'); 
    sleep(1); 
} 
chunk(''); 
?> 

它需要一個空塊在輸出結束時。

那麼你可以通過調用函數chunk這樣簡單的輸出數據:

chunk('data');