I've searched around and found two different ways to define Content-type for JSON file loaded with php.
header('Content-type: text/json');
header('Content-type: application/json');
which one should be used ?
AFIK IANA has officially registered a MIME type for JSON as |
|||||||||||||||
|
For JSON:
For JSON-P:
As @EricCope mentioned, you probably might also want to append |
|||||||||
|
Look at things like the Apache Axis spec. They favor |
|||
|
application/json is favored but usually web browsers don't know what to do with those headers and screws it up. For stuff like jquery, I have seen text/html recommended. If you are having errors pop up (e.g. download dialog box) then try text/html |
|||||||||||||||
|
I recently had a very strange run in with this. I had a form which used jQuery to do an AJAX lookup against a PHP script, and then return the response as JSON, formed using PHP's It was all working fine for a couple of months, then this morning it stopped working... It turns out the javascript couldn't parse the JSON correctly. My original method used eval. I.e.
But that gave me the error:
I then tried using
Using the I checked my JSON was formatted correctly using an online tool - it was. In the end I tried changing the header type I was setting in the PHP script which was being called by jQuery AJAX. I changed it from And both the Very odd and still don't know quite why, but thought I'd post here in case someone runs into a similar issue, or anybody can provide an explanation. |
|||||||||
|
1) JSON :Response is Dynamically generated data, according to the Query Parameters passed in the URL. For Eg : Answer : 2) JSON-P :Response is JSON data, with a function call wrapped around it. For Eg : Answer : |
|||
|
Even we can do like below:
|
|||||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.