2010-07-26 48 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');