php header json content type
Returning JSON from a PHP Script
I want to return JSON from a PHP script.
Do I just echo the result? Do I have to set the Content-Type header?
19 Answers 19
While you’re usually fine without it, you can and should set the Content-Type header:
If I’m not using a particular framework, I usually allow some request params to modify the output behavior. It can be useful, generally for quick troubleshooting, to not send a header, or sometimes print_r the data payload to eyeball it (though in most cases, it shouldn’t be necessary).
A complete piece of nice and clear PHP code returning JSON is:
According to the manual on json_encode the method can return a non-string (false):
Returns a JSON encoded string on success or FALSE on failure.
When this happens echo json_encode($data) will output the empty string, which is invalid JSON.
json_encode will for instance fail (and return false ) if its argument contains a non UTF-8 string.
This error condition should be captured in PHP, for example like this:
Then the receiving end should of course be aware that the presence of the jsonError property indicates an error condition, which it should treat accordingly.
In production mode it might be better to send only a generic error status to the client and log the more specific error messages for later investigation.
Read more about dealing with JSON errors in PHP’s Documentation.
The usage of `header(«Content-type:application/json»);`
file name : bank.php
and in data.php I wrote
And when I added dataType : json« inside the ajax function in bank.php the type changes into object
so what is the function of header(«Content-type:application/json»); actually?
1 Answer 1
The function header(«Content-type:application/json») sends the http json header to the browser to inform him what the kind of a data he expects. You can see all the http headers for each request in your browser (If you are using chrome open developer tools, go to network, adjust the view and reload the page, you will see all requests made by your browser, if you click on any on any of these requests then click on headers you will see the headers of each request).
When you use this function you will notice the http header Content-Type:application/json in the response sent from the server. If you don’t use it the server will send the default which most likely is Content-type:text/html; charset=UTF-8
As @Monty stated you don’t need this function if you added dataType: ‘json’ to your AJAX as Jquery will handle the data even it is sent with text/html header.
Not the answer you’re looking for? Browse other questions tagged php jquery json ajax or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.