JSON 内容类型有许多“标准 ” :
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
我用哪一种,在哪里?我假设安全和浏览器支持问题是一个因素。
相关:如果JSON被REST API送回,什么MIME类型?
JSON 内容类型有许多“标准 ” :
application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json
我用哪一种,在哪里?我假设安全和浏览器支持问题是一个因素。
相关:如果JSON被REST API送回,什么MIME类型?
当前回答
当然,对 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);
}
};
其他回答
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”是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())
}
}
JSON的正确内容类型是应用程序/json UnLESS, 您正在使用JSONP, 也称为JSON with Padding, 实际上是 JavaScript, 所以正确的内容类型将是应用程序/javascript。
只有在使用应用/json作为MIME类型时(截至2011年11月,有最新版本的Chrome、Firefox和Firebug),我才具备以下条件:
当 JSON 从服务器上装入时, Chrome 不再发出警告 。 Firebug 会在回复中添加一个标签, 显示您格式化的 JSON 数据。 如果 MIME 类型不同, 它会显示为“ 响应内容 ” 。
最常见的 MIME 类型是 应用程序/ json 。 以下是 JSON 全部内容类型列表 :
内容-类型:申请/json-Json-Json-内容-类型:在申请/json正式登记之前文本/x-json-Json-正式登记。内容-类型:申请/javascript-Json-P 内容-类型:申请/x-javascript-JavaScript
过时类型 :
内容- 类型: 文本/ javascript - JavaScript 但已经过时。 用于 HTML 属性的旧的互联网探索器版本。 内容- Type: 文本/ x- javascript - JavaScript 媒体类型, 但已经过时