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

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

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

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


当前回答

如果您在客户端拨打 ASP. NET 网络服务, 您必须使用应用程序/ json 才能工作 。 我相信这对 jQuery 和 Ext 框架是一样的 。

其他回答

PHP 开发商使用此功能 :

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

    // Do something here...
?>

PHP 中的应用程序/json 工作非常出色,可以存储数组或对象数据。

我用这个代码在谷歌云存(GCS)上将数据输入JSON,

$context = stream_context_create([
    'gs' => [
        'acl'=>'public-read', 
        'Content-Type' => 'application/json',
    ]
]);

file_put_contents(
    "gs://BUCKETNAME/FILENAME.json", 
    json_encode((object) $array), 
    false, 
    $context
);

要返回数据是直线前进的:

$data = json_decode(file_get_contents("gs://BUCKETNAME/FILENAME.json"));

并非所有内容类型应用程序/json都有效。

如果您正在使用 Ext JS 表格提交到上传文件, 请注意, 服务器响应由浏览器解析, 以创建 < iframe > 的文档 。

如果服务器正在使用 JSON 发送返回对象, 则内容- 类型页眉必须设置为文本/ html , 以便告诉浏览器将文本插入文档正文, 未改变 。

见Ext JS 3.4.0 API文件。

如果您使用 Ubuntu 或 Debian , 并通过 Apache 服务于 .json 文件, 您可能需要以正确的内容类型服务文件 。 我这样做主要是因为我想使用 Firefox 扩展 JSONView

Apache 模块 mod_ mime 将有助于轻松做到这一点。 但是, 您需要编辑文件/ etc/ mime. type 并添加行

application/json json

然后重新启动 Apache :

sudo service apache2 restart

我用下面的用法

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