简体   繁体   English

Codeigniter:无法回显数据

[英]Codeigniter: Unable to echo data

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 . 然后,欢迎您进一步了解:D。

CodeIgniter has an output buffering system (which also allows it to do things like cache controller output, set headers, and collect view output). CodeIgniter有一个输出缓冲系统(它还允许它执行诸如缓存控制器输出,设置标头和收集视图输出之类的操作)。 You don't usually echo from a controller method. 你通常不会echo从控制器的方法。 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 . 有关Output类,请参见CodeIgniter文档

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. 如果这样做没有帮助,请尝试使用视图来显示数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM