What is the correct JSON content type?

2018-12-30 23:44发布

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.

30条回答
裙下三千臣
2楼-- · 2018-12-31 00:05

For JSON:

Content-Type: application/json

For JSON-P:

Content-Type: application/javascript
查看更多
唯独是你
3楼-- · 2018-12-31 00:06

The correct answer is:

Content-Type: application/json
查看更多
忆尘夕之涩
4楼-- · 2018-12-31 00:07

For JSON text:

application/json

The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627).

For JSONP (runnable javascript) with callback:

application/javascript

Here are some blog posts that were mentioned in the comments that are relevant.

查看更多
柔情千种
5楼-- · 2018-12-31 00:07

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 my web.config file for compressing responses.

<httpCompression>
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
    <dynamicTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
    </dynamicTypes>
    <staticTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
    </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>

By using this, the .aspx pages was compressed with g-zip but JSON responses were not. I added

<add mimeType="application/json" enabled="true"/>

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

<add mimeType="application/x-javascript" enabled="true"/>

in both the static and dynamic types sections, and changed the response type in

.ashx (asynchronous handler) to

application/x-javascript

And now I found that my JSON responses were compressed with g-zip. So I personally recommend to use

application/x-javascript

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.

查看更多
倾城一夜雪
6楼-- · 2018-12-31 00:07

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 default text/html.

查看更多
高级女魔头
7楼-- · 2018-12-31 00:08

I use the below

contentType: 'application/json',
data: JSON.stringify(SendData),
查看更多
登录 后发表回答