JSON 内容类型有许多“标准 ” :

application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json

我用哪一种,在哪里?我假设安全和浏览器支持问题是一个因素。

相关:如果JSON被REST API送回,什么MIME类型?


当前回答

内容-类型:应用/json - JSON 内容-类型:应用/javascript-内容-类型:应用/x-javascript 内容-类型:应用/x-javascript - 内容-类型: JavaScript 内容-类型:文本/jawaScript :文本/javascript 但应用/json 正式注册前 JSON - JSON

其他回答

当然,对 JSON 来说正确的 MIME 媒体类型是应用程序/json, 但有必要了解在您的应用程序中需要什么样的数据。

例如,我使用Ext GWT, 服务器响应必须作为文本/ html, 但包含 JSON 数据 。

客户端, 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");
    }
});

如果使用应用程序/json回应类型,浏览器建议我保存文件。

使用 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);
    }
};

JSON的正确内容类型是应用程序/json UnLESS, 您正在使用JSONP, 也称为JSON with Padding, 实际上是 JavaScript, 所以正确的内容类型将是应用程序/javascript。

毫无疑问,申请/json是JSON答复的最佳MIME类型。

但我有一些经验,因为某些压缩问题,我不得不使用应用程序/ X- javascript。我的主机环境与 GoDdddy 共享。 它们不允许我更改服务器配置。 我在我的 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"/>

通过使用这个, . aspx 页面被压缩为 g- zip, 但 JSON 没有回应 。 我添加了 。

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

但这并不压缩JSON的反应。

在那之后,我删除了这个新添加的类型,并添加了

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

在静态和动态两种类型的章节中都采用静态和动态两种类型,并更改了

.ashx (非同步处理器) to

application/x-javascript

现在我发现我的JSON反应 压缩了G-zip。所以我个人建议使用

application/x-javascript

只有在您想要压缩您的 JSON 对共享主机环境的反应时。 因为共享主机不允许您更改 IIS 配置 。

为了补充先前的答复,根据W3C,JSON链接数据(JSON-LD)的MIME类型是:

应用程序/ld+json 类型名称: 应用程序 子类型名称: ld+json

此外,来自同一来源的:

文件扩展名:.jsonld

贾森:

根据在 URL 中传递的查询参数, 回复是动态生成的数据 。

示例:

{ "Name": "Foo", "Id": 1234, "Rank": 7 }

内容类型:申请/json


JSON -P: 贾森 -P:

JSON用垫子。回应是 JSON数据, 并有一个功能电话围绕它。

示例:

functionCall({"Name": "Foo", "Id": 1234, "Rank": 7});

内容-类型:申请/标注