有许多“标准”为“标准”Json Json (Json Json) (Json Json Json) (Json Json) Json Json (Json Json) Json (Json) Json (Json) Json (Json) Json (Json) Json (Json) Json (Json) Json(Json) Json (Json) Json (Json) Json(Json) Json (Json) Json内容类型 :

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

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

相关:如果JSON被AST API送回来 是什么MIME类型?


当前回答

PHP 开发商使用此功能 :

<?php
    header("Content-type: application/json");

    // Do something here...
?>

其他回答

JSON 的正确内容类型是application/json除非你们在使用贾索普,也称为 Json with Padding, 实际上是 JavaScript, 所以正确的内容类型将是application/javascript.

正确的答案是:

Content-Type: application/json

正如许多其他人已经提到的那样,application/json是正确答案。

但还没有被解释的是你提出的其他选择 意指什么

  • application/x-javascript: JavaScript 以前 JavaScript 的实验 MIME 类型application/javascript成为标准。

  • text/javascript:现在过时。你应该使用application/javascript当使用 Javaramps 时。

  • text/x-javascript:上述情况下的实验性MIME类型。

  • text/x-jsonJSON 之前的实验 MIME 类型application/json正式注册。

总的来说,当你对内容类型有任何疑问时,你应该检查此链接

当然,对JSON来说正确的MIME媒体类型是application/json,但有必要了解在您的应用程序中预期会收到何种类型的数据。

例如,我使用Ext GWT 离 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");
    }
});

使用申请/日文响应类型,浏览器建议我保存文件。

服务器侧端源代码片断使用春春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);
    }
};

春春您有一个定义型号 :MediaType.APPLICATION_JSON_VALUE等于申请/日文.