My server is sending data to the client.
header('Content-type: application/json');
echo json_encode($jsondata);
What is the benefit of the header? It encodes the data, but doesn't appear to have any visual effects in the various browsers that I tested it. I've also seen it break a fileupload plugin (Valum's I think but don't recall). Lastly, if my server is only returning true/false (or 1/0, or success/null), should I still encode the response?
So, to recap my question, when and why should Content-Type be application/json?
Simple: the content-type should be application/json whenever you're serving JSON data. It's just a matter of declaring the data properly, like having the right license plates on your car or an ingredients list on a bag of chips.
Whether there is a visual representation of the data in the browser is another issue, that's not the main reason why you serve a content type. (I know of no browser that shows a nice tree view for JSON data. It would be nice though!)
Lastly, if my server is only returning true/false (or 1/0, or success/null), should I still encode the response?
It depends on what the client expects, but the best policy is usually to be consistent in what you output, i.e. always JSON encode it if your other services use JSON encoding as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With