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.
For JSON:
For JSON-P:
The correct answer is:
For JSON text:
application/json
For JSONP (runnable javascript) with callback:
Here are some blog posts that were mentioned in the comments that are relevant.
text/html
for JSONapplication/json
(DEAD 2018-06-26)There is no doubt that
application/json
is the best MIME type for a JSON response.But I had some experience where I had to use
application/x-javascript
because of some compression issues. My hosting environment is shared hosting with GoDaddy. They do not allow me to change server configurations. I had added the following code to myweb.config
file for compressing responses.By using this, the .aspx pages was compressed with g-zip but JSON responses were not. I added
in the static and dynamic types sections. But this does not compress JSON responses at all.
After that I removed this newly added type and added
in both the static and dynamic types sections, and changed the response type in
.ashx (asynchronous handler) to
And now I found that my JSON responses were compressed with g-zip. So I personally recommend to use
only if you want to compress your JSON responses on a shared hosting environment. Because in shared hosting, they do not allow you to change IIS configurations.
If you're in a client-side environment, investigating about the cross-browser support is mandatory for a well supported web application.
The right HTTP Content-Type would be
application/json
, as others already highlighted too, but some clients do not handle it very well, that's why jQuery recommends the defaulttext/html
.I use the below