Codeigniter:无法回显数据 [英] Codeigniter: Unable to echo data

查看:180
本文介绍了Codeigniter:无法回显数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好人,

我的功能有一个奇怪的问题.

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

任何对此的见解都将是受欢迎的:D.

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

推荐答案

CodeIgniter具有输出缓冲系统(它也可以执行缓存控制器输出,设置标头和收集视图输出之类的操作).通常,您不使用控制器方法来echo.改为执行此操作:

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

请参见 Output类的CodeIgniter文档.

这篇关于Codeigniter:无法回显数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆