2014-04-13 17 views
1

我想制作一个流式端点,不断发送json对象。我试过使用分块传输编码,并且能够成功地使所有浏览器(chrome,firefox,safari)呈现从服务器发送的数据,如果它是text/html或text/plain。但是,当我使用application/json作为内容类型时,它不适用于chrome。下面的代码:Chrome不是呈现分块的json响应

var http = require('http'); 

http.createServer(function(request, response){ 
    response.setHeader('Connection', 'Transfer-Encoding'); 
    response.setHeader('Transfer-Encoding', 'chunked'); 
    response.setHeader('Cache-Control', 'no-cache'); 
    // response.setHeader('Content-Type', 'application/json'); 
    response.setHeader('Content-Type', 'text/html'); 


    setInterval(function(){  
     response.write('test </br>'); 
     // response.write('{"test": "test"}'); 
    }, 10); 

}).listen(8000); 

上面的代码工作正常,但不能使之与应用/ JSON类型的工作。我错过了什么吗?

+0

谷歌浏览器以“查看源代码”模式实时显示输出。查看'view-source:http:// localhost:8000 /'。什么是预期的行为? –

+0

这是预期的行为。那么,如果我通过Chrome浏览器查看twitter streaming API,同样的事情会发生吗? – az7ar

回答

2

这是浏览器中的一个错误。并在最新的铬版(至少在金丝雀)固定。

Chrome Bug - Transfer-Encoding chunked not support on text/plain

Safari浏览器(我已经在Mac OSX测试)是不呈现非HTML内容与分块编码的浏览器。

+0

仍然不适合我在Chrome – Jekis

+0

看起来像他们现在修复它。只有Mac上的Safari不能正常工作。 –