I've been messing around with JSON for some time, just pushing it out as text and it hasn't hurt anybody (that I know of), but I'd like to start doing things properly.
I have seen so many purported "standards" for the JSON content type:
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
But which is correct, or best? I gather that there are security and browser support issues varying between them.
I know there's a similar question, What MIME type if JSON is being returned by a REST API?, but I'd like a slightly more targeted answer.
If the JSON is with padding then it will be
application/jsonp
. If the JSON is without padding then it will beapplication/json
.To deal with both, it is a good practice to use: 'application/javascript' without bothering whether it is with padding or without padding.
If you are using Ubuntu or Debian and you serve .json files through Apache, you might want to serve the files with the correct content type. I am doing this primarily because I want to use the Firefox extension JSONView
The Apache module mod_mime will help to do this easily. However, with Ubuntu you need to edit the file /etc/mime.types and add the line
Then restart Apache:
Not everything works for content type
application/json
.If you are using Ext JS form submit to upload file, be aware that the server response is parsed by the browser to create the document for the
<iframe>
.If the server is using JSON to send the return object, then the
Content-Type
header must be set totext/html
in order to tell the browser to insert the text unchanged into the document body.See the Ext JS 3.4.0 API documentation.
Content-type: application/json
- jsonContent-Type: application/javascript
- json-PContent-type: application/x-javascript
- javascriptContent-type: text/javascript
- javascript BUT obsolete, older IE versions used to use as html attribute.Content-type: text/x-javascript
- JavaScript Media Types BUT obsoleteContent-type: text/x-json
- json before application/json got officially registered.Of course, the correct MIME media type for JSON is
application/json
, but it's necessary to realize what type of data is expected in your application.For example, I use Ext GWT and the server response must go as text/html but contains JSON data.
Client side, Ext GWT form listener
In case of using application/json response type, the browser suggests me to save the file.
Server side source code snippet using Spring MVC
JSON:
Response is dynamically generated data, according to the query parameters passed in the URL.
Example:
Content-Type:
application/json
JSON-P:
JSON with padding. Response is JSON data, with a function call wrapped around it.
Example:
Content-Type:
application/javascript