【问题标题】:Chrome is not rendering chunked json responseChrome 没有呈现分块的 json 响应
【发布时间】:2014-04-13 10:41:27
【问题描述】:

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

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);

上面的代码按预期工作,但不能使它与 application/json 类型一起工作。我错过了什么吗?

【问题讨论】:

  • Google Chrome 在“查看源代码”模式下实时呈现输出。见view-source:http://localhost:8000/。预期的行为是什么?
  • 这是预期的行为。那么,如果我通过 chrome 浏览器查看 twitter 流 api,同样的事情会发生吗?

标签: node.js http http-headers http-streaming


【解决方案1】:

这是浏览器中的错误。并在最新的 chrome 中修复(至少在金丝雀中)。

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

Safari(我在 Mac OSX 上测试过)是唯一不使用分块编码呈现非 html 内容的浏览器。

【讨论】:

  • 看起来他们现在修好了。只有 Mac 上的 safari 不工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 2021-05-11
  • 2016-09-26
  • 2014-04-29
  • 2022-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多