Codeigniter: Unable to echo data

随声附和 提交于 2019-12-25 08:25:21

问题


Ok folks,

I have an odd issue with a function of mine.

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());
    }
}

This when accesed directly will not echo anything. By enabling the profiler though it functions fine and outputs the data.

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());
    }
}

Any insight into this would be more then welcome :D .


回答1:


CodeIgniter has an output buffering system (which also allows it to do things like cache controller output, set headers, and collect view output). You don't usually echo from a controller method. Do this instead:

public function mymethod() {
    $anobject = array();
    $output = json_encode($anobject);

    $this->output->set_content_type('application/json');
    $this->output->set_output($output);
}

See the CodeIgniter documentation for the Output class.




回答2:


Maybe you have output buffering or compression enabled-this could cause problems like this. Also check that the variable you're trying to output isn't empty. If this doesn't help, try using a view to display data.



来源:https://stackoverflow.com/questions/8391178/codeigniter-unable-to-echo-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!