什么是正确的 JSON 内容类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/477816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct JSON content type?
提问by Oli
I've been messing around with JSONfor 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.
我一直在搞乱JSON,只是将它作为文本推出,它没有伤害任何人(据我所知),但我想开始正确地做事。
I have seen somany purported "standards" for the JSON content type:
我见过这么多所谓的“标准”为JSON内容类型:
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.
我知道有一个类似的问题,如果 REST API 返回 JSON,那么什么 MIME 类型?,但我想要一个更有针对性的答案。
回答by Gumbo
For JSON text:
对于 JSON 文本:
application/json
application/json
The MIME media type for JSON text is
application/json. The default encoding is UTF-8. (Source: RFC 4627).
JSON 文本的 MIME 媒体类型是
application/json. 默认编码为 UTF-8。(来源:RFC 4627)。
For JSONP(runnable javascript) with callback:
对于带有回调的JSONP(可运行的 javascript):
application/javascript
application/javascript
Here are some blog posts that were mentioned in the comments that are relevant.
以下是评论中提到的一些相关博客文章。
回答by gnrfan
IANAhas registered the official MIME Type for JSON as application/json.
IANA已将 JSON 的官方 MIME 类型注册为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/*.
当被问及为什么不这样做时text/json,Crockford 似乎说 JSON 不是真正的 JavaScript 或文本,而且 IANAapplication/*比text/*.
More resources:
更多资源:
回答by Alix Axel
回答by Mikhail.Mamaev
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.
当然,JSON 的正确 MIME 媒体类型是application/json,但有必要了解您的应用程序中需要什么类型的数据。
For example, I use Ext GWTand the server response must go as text/htmlbut contains JSON data.
例如,我使用Ext GWT并且服务器响应必须以text/html 格式显示,但包含 JSON 数据。
Client side, Ext GWT form listener
客户端,Ext GWT 表单侦听器
uploadForm.getForm().addListener(new FormListenerAdapter()
{
@Override
public void onActionFailed(Form form, int httpStatus, String responseText)
{
MessageBox.alert("Error");
}
@Override
public void onActionComplete(Form form, int httpStatus, String responseText)
{
MessageBox.alert("Success");
}
});
In case of using application/jsonresponse type, the browser suggests me to save the file.
如果使用application/json响应类型,浏览器会建议我保存文件。
Server side source code snippet using Spring MVC
使用Spring MVC 的服务器端源代码片段
return new AbstractUrlBasedView()
{
@SuppressWarnings("unchecked")
@Override
protected void renderMergedOutputModel(Map model, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
response.setContentType("text/html");
response.getWriter().write(json);
}
};
回答by Bhavin
JSON:
JSON:
Response is dynamically generated data, according to the query parameters passed in the URL.
响应是根据 URL 中传递的查询参数动态生成的数据。
Example:
例子:
{ "Name": "Foo", "Id": 1234, "Rank": 7 }
Content-Type:application/json
内容类型:application/json
JSON-P:
JSON-P:
JSON with padding. Response is JSON data, with a function call wrapped around it.
带填充的 JSON。响应是 JSON 数据,其中包含一个函数调用。
Example:
例子:
functionCall({"Name": "Foo", "Id": 1234, "Rank": 7});
Content-Type:application/javascript
内容类型:application/javascript
回答by Gourneau
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
如果您使用的是 Ubuntu 或 Debian 并且您通过 Apache 提供 .json 文件,您可能希望提供具有正确内容类型的文件。我这样做主要是因为我想使用 Firefox 扩展JSONView
The Apache module mod_mimewill help to do this easily. However, with Ubuntu you need to edit the file /etc/mime.typesand add the line
Apache 模块mod_mime将有助于轻松完成此操作。但是,对于 Ubuntu,您需要编辑文件/etc/mime.types并添加行
application/json json
Then restart Apache:
然后重启Apache:
sudo service apache2 restart
回答by markvpc
回答by Resist Design
The right content type for JSON is application/jsonUNLESS you're using JSONP, also known as JSON with Padding, which is actually JavaScript and so the right content type would be application/javascript.
JSON 的正确内容类型是application/json除非您使用JSONP,也称为 JSON with Padding,它实际上是 JavaScript,因此正确的内容类型是application/javascript.
回答by shashwat
There is no doubt that application/jsonis the best MIMEtype for a JSON response.
毫无疑问,这application/json是JSON 响应的最佳MIME类型。
But I had some experience where I had to use application/x-javascriptbecause 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.configfile for compressing responses.
但是application/x-javascript由于一些压缩问题,我有一些不得不使用的经验。我的托管环境是与GoDaddy共享托管。他们不允许我更改服务器配置。我已将以下代码添加到我的web.config文件中以压缩响应。
<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
通过使用它,.aspx 页面被 g-zip 压缩,但 JSON 响应不是。我加了
<add mimeType="application/json" enabled="true"/>
in the static and dynamic types sections. But this does not compress JSON responses at all.
在静态和动态类型部分。但这根本不会压缩 JSON 响应。
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
.ashx(异步处理程序)到
application/x-javascript
And now I found that my JSON responses were compressed with g-zip. So I personally recommend to use
现在我发现我的 JSON 响应是用 g-zip 压缩的。所以我个人推荐使用
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 IISconfigurations.
仅当您想在共享托管环境中压缩 JSON 响应时。因为在共享主机中,它们不允许您更改IIS配置。
回答by Ivo Limmen
Only when using application/jsonas the MIMEtype I have the following (as of November 2011 with the most recent versions of Chrome, Firefox with Firebug):
仅当application/json用作MIME类型时,我才具有以下内容(截至 2011 年 11 月,最新版本的 Chrome、Firefox 和Firebug):
- No more warnings from Chrome when the JSON is loaded from the server.
- Firebug will add a tab to the response showing you the JSON data formatted. If the MIME type is different, it will just show up as 'Response content'.
- 从服务器加载 JSON 时,Chrome 不再发出警告。
- Firebug 会在响应中添加一个选项卡,显示格式化的 JSON 数据。如果 MIME 类型不同,它只会显示为“响应内容”。

