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

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

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

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


当前回答

我用下面的用法

contentType: 'application/json',
data: JSON.stringify(SendData),

其他回答

为具体说明有趣的JSON结果,请在请求页眉中加入“申请/json”,如下文所示:

“接受:申请/json”是一种理想的反应格式。

“ Content-Type: application/json” 指定了您请求的内容格式, 但有时您同时指定了应用程序/ json 和 application/ xml , 但这些格式的质量可能不同 。 哪个服务器会发送不同的响应格式, 请看下面的例子 :

Accept:application/json;q=0.4,application/xml;q=8

这将返回 XML, 因为 XML 质量更高 。

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

“申请/json”是JSON正确的内容类型。

def ajaxFindSystems = {
  def result = Systems.list()
  render(contentType:'application/json') {
    results {
      result.each{sys->
        system(id:sys.id, name:sys.name)
      }
    }
    resultset (rows:result.size())
  }
}

PHP 开发商使用此功能 :

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

    // Do something here...
?>

JSON 文本:

application/json

JSON 文本的 MIME 媒体类型是应用程序/ json。 默认编码是 UTF-8 。 (资料来源: RFC 4627)

JSONP (可运行的 JavaScript) 使用回调 :

application/javascript

以下是相关评论中提到的一些博客文章:

JSON Internet Explorer 为何不使用文本/ html 用于 JSON 的 JSON Internet Explorer 有时与应用程序/ json 有问题,