Ara*_*soi 0 php json codeigniter
好的伙计们,
我的一个功能有一个奇怪的问题.
public function getOutages($Site)
{
// pull a json data dump of all outages
If(!$Site){
echo '[{}]';
}else{
$this->load->database('default', TRUE);
$this->db->where('Clear', '0');
$this->db->where('FracID', $Site);
$query = $this->db->get('vw_Outages');
echo json_encode($query->result_array());
}
}
Run Code Online (Sandbox Code Playgroud)
直接接收时不会回显任何东西.通过启用分析器,它可以正常运行并输出数据.
public function getOutages($Site)
{
$this->output->enable_profiler(TRUE);
// pull a json data dump of all outages
If(!$Site){
echo '[{}]';
}else{
$this->load->database('default', TRUE);
$this->db->where('Clear', '0');
$this->db->where('FracID', $Site);
$query = $this->db->get('vw_Outages');
echo json_encode($query->result_array());
}
}
Run Code Online (Sandbox Code Playgroud)
任何对此的见解都会受到欢迎:D.
CodeIgniter有一个输出缓冲系统(它还允许它执行诸如缓存控制器输出,设置标头和收集视图输出之类的事情).您通常echo不使用控制器方法.改为:
public function mymethod() {
$anobject = array();
$output = json_encode($anobject);
$this->output->set_content_type('application/json');
$this->output->set_output($output);
}
Run Code Online (Sandbox Code Playgroud)
请参阅Output类的CodeIgniter文档.