什么是正确的 JSON 内容类型? [英] What is the correct JSON content type?

查看:23
本文介绍了什么是正确的 JSON 内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 JSON 一段时间,只是将其作为文本推出它没有伤害任何人(据我所知),但我想开始正确地做事.

我见过这么多个所谓的标准"对于 JSON 内容类型:

application/json应用程序/x-javascript文本/javascript文本/x-javascript文本/x-json

但是哪个是正确的,还是最好的?我认为它们之间存在不同的安全和浏览器支持问题.

我知道有一个类似的问题,如果 REST API 返回 JSON,那么什么 MIME 类型?,但我想要一个更有针对性的答案.

解决方案

对于 JSON 文本:

应用程序/json

<块引用>

JSON 文本的 MIME 媒体类型是 application/json.默认编码为 UTF-8.(来源:RFC 4627)

对于带有回调的 JSONP(可运行的 JavaScript):

应用程序/javascript

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

I've been messing around with JSON for some time, just pushing it out as text and it hasn't hurt anybody (that I know of), but I'd like to start doing things properly.

I have seen so many purported "standards" for the JSON content type:

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

But which one is correct, or best? I gather that there are security and browser support issues varying between them.

I know there's a similar question, What MIME type if JSON is being returned by a REST API?, but I'd like a slightly more targeted answer.

解决方案

For JSON text:

application/json

The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627)

For JSONP (runnable JavaScript) with callback:

application/javascript

Here are some blog posts that were mentioned in the relevant comments:

这篇关于什么是正确的 JSON 内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆