有人知道为什么吗$this响应($somearray,200);无法输出长JSON?(或长数组)例如,如果我将查询限制在最后10行,但如果我将其限制在100行,我可以输出DB结果JSON被三聚,在我的客户端应用程序中,我得到了"意外的输入结束"。如果我简单地改变上面的陈述echo json_encode($somearray);它完美地工作。
对不起。我需要澄清我的问题。我正在使用https://github.com/chriskacerguis/codeigniter-restserver
function transits_get()
{
$sessionId = $this->input->get_request_header('Sessionid', TRUE);
$transits = $this->Transit_model->get_transits( $sessionId );
if($transits)
{
//$this->output->set_content_type('application/json');
//$this->output->set_output(json_encode($transits)); //works
//echo json_encode($transits); //works
$this->response($transits, 200); // 200 being the HTTP response code //Does not work
}
else
{
$this->response(array('error' => 'There is no transit records in the database!'), 404);
}
}
好吧,我找到了问题所在。我只是简单地注释掉了REST_controller中设置内容长度的代码。现在它工作了,但我仍然想知道为什么对于较大的JSON文件,strlen($output)的长度与输出JSON的长度不同,而对于较短的JSON文件则可以。
// If zlib.output_compression is enabled it will compress the output,
// but it will not modify the content-length header to compensate for
// the reduction, causing the browser to hang waiting for more data.
// We'll just skip content-length in those cases.
if ( ! $this->_zlib_oc && ! $this->config->item('compress_output')) {
//header('Content-Length: ' . strlen($output));
}
这对我很有用,我有很多JSON数据(+-250条记录):
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($data));
如本文所述,您可以执行以下操作:
打开REST_Controller.php
定位并删除以下代码(在响应功能的末尾):
if ( ! $this->_zlib_oc && ! $CFG->item('compress_output')) { header('Content-Length: ' . strlen($output)); }
在库文件夹中,REST_Controller.php文件的第267行,注释以下代码。它起作用了。
header('Content-Length: ' . strlen($output));