我在CakePHP 2.x中有一个遗留应用程序
它在控制器中有一个方法,该方法以如下结构输出JSON:
{"id":59,"name":"Association of Southeast Asian Nations (ASEAN)","n_grouptags":1}
使用的控制器方法
$this->response->type('json');
设置
content-type
application/json; charset=UTF-8
. 一切都好。
我注意到,如果返回的数据超过了一定的长度
内容类型
text/html; charset=UTF-8
代码没有任何更改
.
少量数据(内容类型=
application/json
-预期的):
text/html
-意外的):
https://jsonlint.com/
这是为什么?这取决于浏览器如何处理响应的长度,还是这是一个CakePHP问题?
负责输出的PHP如下-
但是在上面给出的两个不同的输出之间并没有改变
:
$this->autoRender = false; // No View (template) is associated with this
$out = []; // Reset
// $tags is some data from a model
foreach ($tags as $k => $v) {
$n_grouptags = 123; // Actual number comes from a Model
$out[] = ['id' => $k, 'name' => $v, 'n_grouptags' => $n_grouptags];
}
$this->response->type('json'); // We *want* a JSON response
echo json_encode($out, JSON_FORCE_OBJECT); // Encode $out (the output) as JSON
应用程序中的缓存被禁用:
Configure::write('Cache.disable', true);