public function jsonExit($array)
{
$this->con->output->set_content_type('application/json'); // $this->con is get_instance from the constructor
echo json_encode($array);
}
这段代码以JSON格式正确输出数据。但是,当我将其包含exit;
在函数中时,内容类型将text/html
代替application/json
我定义的内容类型。
是什么原因?我可以用什么代替exit
?在这种情况下return
,它将停止,仅停止执行此功能jsonExit
,因此将不起作用。但是它将继续从我调用jsonExit函数的位置运行脚本。我的任务是完全退出。
这是因为您直接使用echo
。
而是使用set_output
。文件在这里
public function jsonExit($array)
{
$this->con->output->set_content_type('application/json'); // $this->con is get_instance from the constructor
$this->con->output->set_output(json_encode($array));
}
如果需要exit
或die
使用_display
。文件在这里
在脚本执行结束时会自动调用此方法,除非您在代码中使用exit()或die()中止脚本执行,否则无需手动调用它。
public function jsonExit($array)
{
$this->con->output->set_content_type('application/json'); // $this->con is get_instance from the constructor
$this->con->output->_display(json_encode($array));
exit(0);
}
或在示例中使用
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句