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类型?
当前回答
如果您使用 Ubuntu 或 Debian , 并通过 Apache 服务于 .json 文件, 您可能需要以正确的内容类型服务文件 。 我这样做主要是因为我想使用 Firefox 扩展 JSONView
Apache 模块 mod_ mime 将有助于轻松做到这一点。 但是, 您需要编辑文件/ etc/ mime. type 并添加行
application/json json
然后重新启动 Apache :
sudo service apache2 restart
其他回答
如果JSON用垫子,那将是申请/jsonp。如果JSON没有划线,那将是申请/json。
要同时处理这两种问题,使用“应用/刺杀”是一种良好做法,无需担心是用垫子还是不用垫子。
正确的答案是:
Content-Type: application/json
JSON 文本:
application/json
JSON 文本的 MIME 媒体类型是应用程序/ json。 默认编码是 UTF-8 。 (资料来源: RFC 4627)
JSONP (可运行的 JavaScript) 使用回调 :
application/javascript
以下是相关评论中提到的一些博客文章:
JSON Internet Explorer 为何不使用文本/ html 用于 JSON 的 JSON Internet Explorer 有时与应用程序/ json 有问题,
如果您在客户端拨打 ASP. NET 网络服务, 您必须使用应用程序/ json 才能工作 。 我相信这对 jQuery 和 Ext 框架是一样的 。
当然,对 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);
}
};