问题描述
我一直在使用 JSON 一段时间,只是将其作为文本推出它没有伤害任何人(据我所知),但我想开始正确地做事.
我见过这么多个所谓的标准"对于 JSON 内容类型:
application/json应用程序/x-javascript文本/javascript文本/x-javascript文本/x-json
但是哪个是正确的,还是最好的?我认为它们之间存在不同的安全和浏览器支持问题.
我知道有一个类似的问题,如果 REST API 返回 JSON,那么什么 MIME 类型?,但我想要一个更有针对性的答案.
对于 JSON 文本:
应用程序/json
JSON 文本的 MIME 媒体类型是 application/json
.默认编码为 UTF-8.
对于带有回调的 JSONP(可运行的 JavaScript):
应用程序/javascript
以下是相关评论中提到的一些博客文章:
- 为什么不应该将
text/html
用于 JSON - Internet Explorer 有时会遇到
application/json
的问题 - 相当完整的 Mimetypes 列表以及它们的用途
- IANA 的官方 mime 类型列表来自@gnrfan 下面的回答
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 one 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 text:
application/json
For JSONP (runnable JavaScript) with callback:
application/javascript
Here are some blog posts that were mentioned in the relevant comments:
- Why you shouldn't use
text/html
for JSON - Internet Explorer sometimes has issues with
application/json
- A rather complete list of Mimetypes and what to use them for
- The official mime type list at IANA from @gnrfan's answer below
这篇关于什么是正确的 JSON 内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!