gpt4 book ai didi

php - 非常长的 json 响应停止并以文本形式发送 HTTP header ,然后继续

转载 作者:可可西里 更新时间:2023-11-01 13:23:21 26 4
gpt4 key购买 nike

我完全迷失了这个问题。

我有一个获取 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

...代表更多相同的"{"test":"test length"},"字符串

所以响应似乎是这样的:

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

没有确切的响应长度会发生这种情况,但是 360791 个字符就可以了,但 372797 个字符就不行了。

我使用的是 Yii PHP 框架,但搜索了很远,但在论坛上没有看到任何东西。

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

或者可能有一个最大的返回值?

编辑_____________________________________

我已经按照建议尝试了 application/json 内容类型,但它仍在发生。 body中返回的header的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

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

**编辑 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

所以看起来它不再作为分块发送。然而,同样的问题存在 - 响应有内容然后打印出标题然后更多内容。

**现在唯一的区别是响应中没有“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;

最佳答案

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

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

关于php - 非常长的 json 响应停止并以文本形式发送 HTTP header ,然后继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7503991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com