public function jsonExit($array)
{
$this->con->output->set_content_type('application/json'); // $this->con is get_instance from the constructor
echo json_encode($array);
}
This piece of code outputs the data properly in JSON format. But when I include exit;
in the function, the content type becomes text/html
instead of application/json
which I defined.
What is the reason? And with what I can replace the exit
? In this case return
will not work as it stops only executing this function jsonExit
anymore. But it will continue to run the script from where I call the jsonExit function. My task is to EXIT completely.
It is because you are using directly the echo
.
Instead use the set_output
. Docs here
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));
}
If you need the exit
or die
use _display
. Docs here
This method is called automatically at the end of script execution, you won’t need to call it manually unless you are aborting script execution using exit() or die() in your code.
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);
}
Or as it is used in the example
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments