什么是正确的JSON内容类型?

本文讨论了JSON内容类型的正确选择,包括 和 。虽然 普遍被认为是最佳实践,但在特定情况下,如压缩问题或JSONP使用时,可能需要考虑其他类型。文章还提到浏览器兼容性和在共享主机环境下压缩JSON响应的注意事项。
摘要由CSDN通过智能技术生成

本文翻译自:What is the correct JSON content type?

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. 我一直在弄乱JSON一段时间,只是将其作为文本推出,并没有伤害任何人(据我所知),但是我想正确地做事。

I have seen so many purported "standards" for the JSON content type: 我见过这么多的所谓的“标准”为JSON内容类型:

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? 我知道有一个类似的问题, 如果REST API返回JSON,则为哪种MIME类型? , but I'd like a slightly more targeted answer. ,但我想要一个更有针对性的答案。


#1楼

参考:https://stackoom.com/question/20Ii/什么是正确的JSON内容类型


#2楼

JSON的正确内容类型是application/json除非您使用JSONP ,也称为JSON与Padding,它实际上是JavaScript,因此正确的内容类型将是application/javascript


#3楼

There is no doubt that application/json is the best MIME type for a JSON response. 毫无疑问, application/json是JSON响应的最佳MIME类型。

But I had some experience where I had to use application/x-javascript because of some compression issues. 但是由于一些压缩问题,我有一些必须使用application/x-javascript经验。 My hosting environment is shared hosting with GoDaddy . 我的托管环境是与GoDaddy共享的托管。 They do not allow me to change server configurations. 它们不允许我更改服务器配置。 I had added the following code to my web.config file for compressing responses. 我已将以下代码添加到我的web.config文件中以压缩响应。

<httpCompression>
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
    <dynamicTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
    </dynamicTypes>
    <staticTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
    </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>

By using this, the .aspx pages was compressed with g-zip but JSON responses were not. 通过使用此选项,.aspx页使用g-zip压缩,但JSON响应未压缩。 I added 我加了

<add mimeType="application/json" enabled="true"/>

in the static and dynamic types sections. 在静态和动态类型部分中。 But this does not compress JSON responses at all. 但这根本不会压缩JSON响应。

After that I removed this newly added type and added 之后,我删除了这个新添加的类型并添加了

<add mimeType="application/x-javascript" enabled="true"/>

in both the static and dynamic types sections, and changed the response type in 在静态和动态类型部分中,并更改了响应类型

.ashx (asynchronous handler) to .ashx(异步处理程序)

application/x-javascript

And now I found that my JSON responses were compressed with g-zip. 现在,我发现我的JSON响应已使用g-zip压缩。 So I personally recommend to use 所以我个人建议使用

application/x-javascript

only if you want to compress your JSON responses on a shared hosting environment . 仅当您要在共享托管环境上压缩JSON响应时。 Because in shared hosting, they do not allow you to change IIS configurations. 因为在共享主机中,它们不允许您更改IIS配置。


#4楼

If you're in a client-side environment, investigating about the cross-browser support is mandatory for a well supported web application. 如果您在客户端环境中,则对于良好支持的Web应用程序,必须调查跨浏览器的支持。

The right HTTP Content-Type would be application/json , as others already highlighted too, but some clients do not handle it very well, that's why jQuery recommends the default text/html . 正确的HTTP Content-Type将是application/json ,因为其他application/json也已经突出显示,但是某些客户端不能很好地处理它,因此jQuery建议使用默认的text/html


#5楼

JSON is a domain-specific language (DSL) and a data format independent of JavaScript, and as such has its own MIME type, application/json . JSON是一种域特定语言 (DSL)和一种独立于JavaScript的数据格式,因此具有其自己的MIME类型application/json Respect for MIME types is of course client driven, so text/plain may do for transfer of bytes, but then you would be pushing up interpretation to the vendor application domain unnecessarily - application/json . 对MIME类型的尊重当然是由客户端驱动的,因此text/plain可能会用于字节传输,但随后您将不必要地将解释推向供应商应用程序域-application application/json Would you transfer XML via text/plain ? 您会通过text/plain传输XML吗?

But honestly, your choice of MIME type is advice to the client as to how to interpret the data- text/plain or text/HTML (when it's not HTML) is like type erasure- it's as uninformative as making all your objects of type Object in a typed language. 但老实说,您选择的MIME类型是向客户端提供有关如何解释数据的建议- text/plain text/HTMLtext/HTML (当不是HTML时)就像类型擦除-它与使所有对象成为Object类型一样没有信息性以一种打字语言。

No browser runtime I know of will take a JSON document and automatically make it available to the runtime as a JavaScript accessible object without intervention, but if you are working with a crippled client, that's an entirely different matter. 据我所知,没有浏览器运行时会使用JSON文档并自动将其作为JavaScript可访问的对象供运行时使用,而无需干预,但是如果您使用的是残废的客户端,那就完全不同了。 But that's not the whole story- RESTful JSON services often don't have JavaScript runtimes, but it doesn't stop them using JSON as a viable data interchange format. 但是,这并不是整个故事- REST风格的 JSON服务往往没有JavaScript的运行时间,但使用JSON作为一种可行的数据交换格式它不会阻止他们。 If clients are that crippled... then I would consider perhaps HTML injection via an Ajax templating service instead. 如果客户端瘫痪了……那么我可能会考虑通过Ajax模板服务进行HTML注入。

Application/JSON! 应用程序/ JSON!


#6楼

For JSON: 对于JSON:

Content-Type: application/json

For JSON-P : 对于JSON-P

Content-Type: application/javascript
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值