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:27

The right content type for JSON is application/json UNLESS you're using JSONP, also known as JSON with Padding, which is actually JavaScript and so the right content type would be application/javascript.

查看更多
只若初见
3楼-- · 2018-12-31 00:27

JSON (JavaScript Object Notation) and JSONP ("JSON with padding") formats seems to be very similar and therefor it might be very confusing which MIME type they should be using. Even though the formats seems to be very similar, there are some subtle differences between them.

So whenever in any doubts, I have very simple approach (which works perfectly find in most cases), namely, go and check corresponding RFC document.

JSON RFC 4627 (The application/json Media Type for JavaScript Object Notation (JSON)) is a specifications of JSON format. It says in section 6, that the MIME media type for JSON text is

application/json.

JSONP JSONP ("JSON with padding") is handled different way than JSON, in a browser. JSONP is treated as a regular JavaScript script and therefor it should use application/javascript, the current official MIME type for JavaScript. In many cases, however, text/javascript MIME type will work fine too.

Note that text/javascript has been marked as obsolete by RFC 4329 (Scripting Media Types) document and it is recommended to use application/javascript type instead. However, due to legacy reasons, text/javascript is still widely used and it has cross-browser support (which is not always a case with application/javascript MIME type, especially with older browsers).

查看更多
千与千寻千般痛.
4楼-- · 2018-12-31 00:29

IANA has registered the official MIME Type for JSON as application/json.

When asked about why not text/json, Crockford seems to have said JSON is not really JavaScript nor text and also IANA was more likely to hand out application/* than text/*.

More resources:

查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 00:29

As many others have mentioned, application/json is the correct answer.

But what haven't been explained yet is what the other options you proposed mean.

  • application/x-javascript: Experimental MIME type for JavaScript before application/javascript was made standard.

  • text/javascript: Now obsolete. You should use application/javascript when using javascript.

  • text/x-javascript: Experimental MIME type for the above situation.

  • text/x-json: Experimental MIME type for JSON before application/json got officially registered.

All in all, whenever you have any doubts about content types, you should check this link

查看更多
笑指拈花
6楼-- · 2018-12-31 00:29

For JSON, I am using:

 Content-Type: application/json

This is described in the IETF's JSON Data Interchange Format 7158 proposal, Section 1.2: Specifications of JSON.

查看更多
妖精总统
7楼-- · 2018-12-31 00:30

If you get data from REST API in JSON so you have to use content-type

For JSON data: Content-Type:application/json
For HTML data: Content-Type:text/html,
For XHTML data: Content-Type:application/xhtml+xml,
For XML data: Content-Type:text/xml, application/xml
查看更多
登录 后发表回答