Using Exit in CodeIgniter output makes the content type header to text/html instead of the defined one

Sarvap Praharanayuthan
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.

Riccardo

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 exitor dieuse _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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Exit in CodeIgniter output makes the content type header to text/html instead of the defined one

From Dev

header('Content-type: image/png'); makes the browser all black

From Dev

Use of exit instead of echo changes output mime type

From Dev

Setting default Content Output Type Header in Axis2

From Dev

Setting default Content Output Type Header in Axis2

From Dev

Output type is not defined

From Java

Rails 6.1 will return Content-Type header without modification ... use `#media_type` instead

From Dev

Wrong Content-Type header generated using MultipartFormDataContent

From Dev

jQuery: Content type header is not set

From Dev

Set Content-Type header

From Dev

using a struct that is defined in another header

From Dev

instead of {% block content %} I'm using {% block sub-header%} under navbar, isnt this valid?

From Dev

What makes the C# compiler output an expression tree instead of code?

From Dev

Enabling Spring Security makes Swagger output text/plain instead of HTML

From Dev

Enabling Spring Security makes Swagger output text/plain instead of HTML

From Dev

Using a header of headers instead of repeating

From Dev

Same type defined in two header files

From Dev

Robot Framework makes two instances from the class instead of one

From Dev

Robot Framework makes two instances from the class instead of one

From Dev

How can I make an HTTP request with no Content-Type header using django.test.Client?

From Dev

Can't get at Content-Type header using Express 4 Router

From Dev

How do I fix an invalid content-type header in Twilio using node.js?

From Dev

How to set "Content-type" header in RestEasy Client framework using a proxy interface?

From Dev

Can't get at Content-Type header using Express 4 Router

From Dev

How to set "Content-type" header in RestEasy Client framework using a proxy interface?

From Dev

Cannot map content-type header using int-http:outbound-gateway

From Dev

Git diff: Write output and exit instead of interactive mode

From Dev

How to set Content-Type in Codeigniter

From Dev

Using variable instead of a cat output

Related Related

  1. 1

    Using Exit in CodeIgniter output makes the content type header to text/html instead of the defined one

  2. 2

    header('Content-type: image/png'); makes the browser all black

  3. 3

    Use of exit instead of echo changes output mime type

  4. 4

    Setting default Content Output Type Header in Axis2

  5. 5

    Setting default Content Output Type Header in Axis2

  6. 6

    Output type is not defined

  7. 7

    Rails 6.1 will return Content-Type header without modification ... use `#media_type` instead

  8. 8

    Wrong Content-Type header generated using MultipartFormDataContent

  9. 9

    jQuery: Content type header is not set

  10. 10

    Set Content-Type header

  11. 11

    using a struct that is defined in another header

  12. 12

    instead of {% block content %} I'm using {% block sub-header%} under navbar, isnt this valid?

  13. 13

    What makes the C# compiler output an expression tree instead of code?

  14. 14

    Enabling Spring Security makes Swagger output text/plain instead of HTML

  15. 15

    Enabling Spring Security makes Swagger output text/plain instead of HTML

  16. 16

    Using a header of headers instead of repeating

  17. 17

    Same type defined in two header files

  18. 18

    Robot Framework makes two instances from the class instead of one

  19. 19

    Robot Framework makes two instances from the class instead of one

  20. 20

    How can I make an HTTP request with no Content-Type header using django.test.Client?

  21. 21

    Can't get at Content-Type header using Express 4 Router

  22. 22

    How do I fix an invalid content-type header in Twilio using node.js?

  23. 23

    How to set "Content-type" header in RestEasy Client framework using a proxy interface?

  24. 24

    Can't get at Content-Type header using Express 4 Router

  25. 25

    How to set "Content-type" header in RestEasy Client framework using a proxy interface?

  26. 26

    Cannot map content-type header using int-http:outbound-gateway

  27. 27

    Git diff: Write output and exit instead of interactive mode

  28. 28

    How to set Content-Type in Codeigniter

  29. 29

    Using variable instead of a cat output

HotTag

Archive