很长的json响应停止并发送HTTP标头作为文本然后继续

Jul*_*n S 6 php ajax json http http-headers

我完全迷失了这个问题.

我有一个获取json响应的ajax查询.查询在大多数情况下都能正常工作,但是当json响应非常大时,它似乎会绊倒.

问题是响应最终形式如下:

...est":"test length"}]]}
HTTP/1.1 200 OK
Date: Wed, 21 Sep 2011 17:10:32 GMT
Server: Apache/2.2.11 (Win32) mod_ssl/2.2.11 OpenSSL/0.9.8k PHP/5.3.0
X-Powered-By: PHP/5.3.0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=5, max=90
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

5b03d
{"ResultsInfo":{"RequestID":"131661886010","FeedCompletion":{"0":"100"}},"ResultsData":[[{"test":"test length"},{"test":"test length"},

...0
Run Code Online (Sandbox Code Playgroud)

...表示更多相同的"{"test":"test length"},"string

所以响应似乎是以下形式:

  • 最后一部分数据
  • http响应标题打印在正文中
  • 人物'5b03d'
  • 第一部分数据
  • 字符'0'

没有确切的响应长度,但是它在360791个字符处是正常的但不是在372797个字符处.

我正在使用Yii PHP框架,但搜索范围广泛,并没有在论坛中看到任何东西.

在我看来,网络服务器正在将响应分成几个部分,或者超时并重新开始.

或者可能有最大的回报?

编辑 ___ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ___

我已按照建议尝试了application/json内容类型,但它正在发生.在正文中返回的标题的Tes文本部分如下(当使用applciaiton/json编码时):

HTTP/1.1 200 OK
Date: Thu, 22 Sep 2011 08:48:28 GMT
Server: Apache/2.2.11 (Win32) mod_ssl/2.2.11 OpenSSL/0.9.8k PHP/5.3.0
X-Powered-By: PHP/5.3.0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=5, max=89
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
Run Code Online (Sandbox Code Playgroud)

如何关闭此特定脚本的分块编码?

**编辑2_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

我现在已经为我的标题添加了一个内容长度,我得到的响应标题仍然在正文中打印出来:

HTTP/1.1 200 OK
Date: Thu, 22 Sep 2011 11:55:39 GMT
Server: Apache/2.2.11 (Win32) mod_ssl/2.2.11 OpenSSL/0.9.8k PHP/5.3.0
X-Powered-By: PHP/5.3.0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 372797
Keep-Alive: timeout=5, max=90
Connection: Keep-Alive
Content-Type: application/json
Run Code Online (Sandbox Code Playgroud)

所以看起来它不再像chunked一样被发送.但是存在同样的问题 - 响应有内容然后打印出标题然后更多内容.

**现在唯一的区别是它在响应中没有'5b03d'或'0'字符.

EDIT_3_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

这里要求的是我的PHP代码的摘要

$dataArray = array(
    'ResultsData'=>array(
          array('test'=>'test length'),
          array('test'=>'test length'),
          array('test'=>'test length'),
          ...
));

$return = json_encode($dataArray);

header('Content-Length: '.strlen($return)); 
header('Content-type: application/json');

echo $return;
Run Code Online (Sandbox Code Playgroud)

rom*_*man 2

您在这里看到的是分块传输编码 - http://en.wikipedia.org/wiki/Chunked_transfer_encoding

这会导致与 text/html 内容类型结合出现问题。将内容类型设置为 application/json 应该可以解决问题。