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

时间:2021-08-02 16:29:12

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 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.

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

34 个解决方案

#1


8968  

For JSON text:

JSON文本:

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

JSON文本的MIME媒体类型是应用程序/ JSON。默认编码是UTF-8。(来源:RFC 4627)。

For JSONP with callback:

对JSONP回调:

application/javascript

应用程序/ javascript

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

以下是一些在评论中提到的博客文章。

#2


1435  

IANA has registered the official MIME Type for JSON as application/json.

IANA已经注册了JSON作为应用程序/ JSON的正式MIME类型。

When asked about why not text/json, Crockford seems to have said JSON is not really JavaScript nor text and also IANA was more likely to hand out application/* than text/*.

当被问及为什么不使用文本/json时,Crockford似乎说json不是真正的JavaScript或文本,而且IANA更可能分发应用程序/*而不是文本/*。

More resources:

更多资源:

#3


766  

For JSON:

JSON:

Content-Type: application/json

For JSON-P:

JSON-P:

Content-Type: application/javascript

#4


570  

Of course, the correct MIME media type for JSON is application/json, but it's necessary to realize what type of data is expected in your application.

当然,对于JSON来说,正确的MIME媒体类型是应用程序/ JSON,但有必要了解应用程序中期望的数据类型。

For example, I use Ext GWT and the server response must go as text/html but contains JSON data.

例如,我使用Ext GWT,而服务器响应必须作为文本/html,但包含JSON数据。

Client side, Ext GWT form listener

客户端,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");
    }
});

In case of using application/json response type, the browser suggests me to save the file.

在使用应用程序/json响应类型时,浏览器建议我保存文件。

Server side source code snippet using Spring MVC

使用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);
    }
};

#5


423  

JSON:

Response is dynamically generated data, according to the query parameters passed in the URL.

响应是动态生成的数据,根据在URL中传递的查询参数。

Example:

例子:

{ "Name": "Foo", "Id": 1234, "Rank": 7 }

Content-Type: application/json

内容类型:application / json


JSON-P:

JSON with padding. Response is JSON data, with a function call wrapped around it.

JSON和填充。响应是JSON数据,它包含一个函数调用。

Example:

例子:

functionCall({"Name": "Foo", "Id": 1234, "Rank": 7});

Content-Type: application/javascript

内容类型:应用程序/ javascript

#6


366  

If you are using Ubuntu or Debian and you serve .json files through Apache, you might want to serve the files with the correct content type. I am doing this primarily because I want to use the Firefox extension JSONView

如果您正在使用Ubuntu或Debian,并且通过Apache提供.json文件,那么您可能希望使用正确的内容类型来服务文件。我这样做主要是因为我想使用Firefox扩展JSONView。

The Apache module mod_mime will help to do this easily. However, with Ubuntu you need to edit the file /etc/mime.types and add the line

Apache模块mod_mime将帮助您轻松完成这一任务。但是,使用Ubuntu,您需要编辑文件/etc/ mime。键入并添加该行。

application/json json

Then restart Apache:

然后重新启动Apache:

sudo service apache2 restart

#7


354  

If you're calling ASP.NET Web Services from the client-side you have to use application/json for it to work. I believe this is the same for the jQuery and Ext frameworks.

如果你调用ASP。来自客户端的网络Web服务必须使用应用程序/json才能工作。我相信这对于jQuery和Ext框架是一样的。

#8


283  

The right content type for JSON is application/json UNLESS you're using JSONP, also known as JSON with Padding, which is actually JavaScript and so the right content type would be application/javascript.

JSON的正确内容类型是应用程序/ JSON,除非您使用的是JSONP,也就是JSON和填充,这实际上是JavaScript,所以正确的内容类型是应用程序/ JavaScript。

#9


271  

There is no doubt that application/json is the best MIME type for a JSON response.

毫无疑问,应用程序/json是json响应的最佳MIME类型。

But I had some experience where I had to use application/x-javascript because of some compression issues. My hosting environment is shared hosting with GoDaddy. They do not allow me to change server configurations. I had added the following code to my web.config file for compressing responses.

但是我有一些经验,因为一些压缩问题,我不得不使用应用程序/x-javascript。我的托管环境与GoDaddy共享托管。它们不允许我更改服务器配置。我在我的网站上添加了以下代码。压缩响应的配置文件。

<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. I added

通过使用这个,.aspx页被压缩了g-zip,但是JSON响应没有。我添加了

<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. So I personally recommend to use

现在我发现我的JSON响应是用g-zip压缩的。所以我个人推荐使用。

application/x-javascript

only if you want to compress your JSON responses on a shared hosting environment. Because in shared hosting, they do not allow you to change IIS configurations.

只有在共享的宿主环境中压缩JSON响应时才可以。因为在共享主机中,它们不允许您更改IIS配置。

#10


249  

Only when using application/json as the MIME type I have the following (as of November 2011 with the most recent versions of Chrome, Firefox with Firebug):

只有在使用应用程序/json作为MIME类型时,我才有如下(截至2011年11月的最新版本的Chrome, Firefox和Firebug):

  • No more warnings from Chrome when the JSON is loaded from the server.
  • 当从服务器加载JSON时,不再有来自Chrome的警告。
  • Firebug will add a tab to the response showing you the JSON data formatted. If the MIME type is different, it will just show up as 'Response content'.
  • Firebug将添加一个选项卡,以显示格式化的JSON数据。如果MIME类型不同,它将显示为“响应内容”。

#11


224  

Not everything works for content type application/json.

并不是所有内容都适合于内容类型应用程序/json。

If you are using Ext JS form submit to upload file, be aware that the server response is parsed by the browser to create the document for the <iframe>.

如果您使用Ext JS表单提交上传文件,请注意服务器响应被浏览器解析以创建

If the server is using JSON to send the return object, then the Content-Type header must be set to text/html in order to tell the browser to insert the text unchanged into the document body.

如果服务器使用JSON发送返回对象,则必须将Content-Type报头设置为text/html,以便告诉浏览器将文本插入到文档主体中。

See the Ext JS 3.4.0 API documentation.

参见Ext JS 3.4.0 API文档。

#12


211  

JSON is a domain-specific language (DSL) and a data format independent of JavaScript, and as such has its own MIME type, 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. Would you transfer XML via text/plain?

JSON是一种特定于领域的语言(DSL)和独立于JavaScript的数据格式,因此它有自己的MIME类型、应用程序/ JSON。对于MIME类型的尊重当然是客户端驱动的,所以文本/plain可能用于传输字节,但是您可能会将对供应商应用程序域的解释推到不必要的应用程序/json中。是否可以通过文本/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类型的选择是对客户的建议,比如如何解释数据——文本/纯文本/文本/HTML(当它不是HTML的时候)就像类型的删除——它的信息不丰富,就像用类型化的语言制作所有类型的对象一样。

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. 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. If clients are that crippled... then I would consider perhaps HTML injection via an Ajax templating service instead.

我所知道的任何浏览器运行时都不会使用JSON文档,并自动将其作为JavaScript可访问对象提供给运行时,而不需要进行干预,但如果您正在与一个残疾客户打交道,那完全是另一回事。但这并不是整个故事——RESTful JSON服务通常没有JavaScript运行时,但它并没有阻止他们使用JSON作为一种可行的数据交换格式。如果客户是残障人士……然后,我将通过Ajax模板服务来考虑HTML注入。

Application/JSON!

Application / JSON !

#13


195  

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内容类型将是应用程序/json,其他的也已经高亮显示了,但是有些客户机处理得不太好,这就是jQuery推荐默认文本/html的原因。

#14


152  

The correct answer is:

正确的答案是:

Content-Type: application/json

#15


151  

As many others have mentioned, application/json is the correct answer.

正如其他许多人提到的,应用程序/json是正确的答案。

But what haven't been explained yet is what the other options you proposed mean.

但还没有解释的是,你提出的其他选择是什么意思。

  • application/x-javascript: Experimental MIME type for JavaScript before application/javascript was made standard.

    应用/x-javascript:在应用程序/ JavaScript之前进行JavaScript的实验MIME类型。

  • text/javascript: Now obsolete. You should use application/javascript when using javascript.

    text / javascript:现在过时了。在使用javascript时,应该使用应用程序/javascript。

  • text/x-javascript: Experimental MIME type for the above situation.

    文本/x-javascript:上述情况的实验MIME类型。

  • text/x-json: Experimental MIME type for JSON before application/json got officially registered.

    文本/x-json:在应用程序/ JSON正式注册之前的JSON实验MIME类型。

All in all, whenever you have any doubts about content types, you should check this link

总之,当您对内容类型有任何疑问时,您应该检查这个链接。

#16


135  

In JSP, you can use this in page directive:

在JSP中,您可以在页面指令中使用它:

<%@ page language="java" contentType="application/json; charset=UTF-8"
    pageEncoding="UTF-8"%>

The correct MIME media type for JSON is application/json. JSP will use it for sending a response to the client.

JSON的正确MIME媒体类型是应用程序/ JSON。JSP将使用它向客户机发送响应。

#17


103  

application/json” is the correct JSON content type.

“应用程序/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())
  }
}

#18


93  

The IANA registration for application/json says

应用程序/json的IANA注册说明。

Applications that use this media type: JSON has been used to exchange data between applications written in all of these programming languages: ActionScript, C, C#, Clojure, ColdFusion, Common Lisp, E, Erlang, Go, Java, JavaScript, Lua, Objective CAML, Perl, PHP, Python, Rebol, Ruby, Scala, and Scheme.

使用这种媒体类型的应用程序:JSON用于在所有这些编程语言编写的应用程序之间交换数据:ActionScript、C、c#、Clojure、ColdFusion、Common Lisp、E、Erlang、Go、Java、JavaScript、Lua、Objective CAML、Perl、PHP、Python、Rebol、Ruby、Scala和Scheme。

You'll notice that IANA.org doesn't list any of these other media types, in fact even application/javascript is now obsolete. So application/json is really the only possible correct answer.

您会注意到,IANA.org没有列出任何其他的媒体类型,事实上,甚至应用程序/javascript现在已经过时了。因此,应用程序/json是唯一可能的正确答案。

Browser support is another thing.

浏览器支持是另一回事。

The most widely supported non-standard media types are text/json or text/javascript. But some big names even use text/plain.

最广泛支持的非标准媒体类型是文本/json或文本/javascript。但是一些大的名字甚至使用文本/普通。

Even more strange is the Content-Type header sent by Flickr, who returns JSON as text/xml. Google uses text/javascript for some of it's ajax apis.

更奇怪的是Flickr发送的Content-Type头文件,它将JSON作为文本/xml返回。谷歌为一些ajax api使用了文本/javascript。

Examples:

例子:

curl -I "https://ajax.googleapis.com/ajax/services/search/video?v=1.0&q=jsonexample"

Output: Content-Type: text/javascript

输出:的content - type:text / javascript

curl -I "https://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&api_key=f82254c1491d894f1204d8408f645a93"

Output: Content-Type: text/xml

输出:的content - type:text / xml

#19


79  

The right MIME type is application/json

正确的MIME类型是应用程序/json。

BUT

I experienced many situations where the browser type or the framework user needed:

我经历了许多情况,其中浏览器类型或框架用户需要:

text/html

application/javascript

#20


67  

I use the below

我使用下面的

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

#21


60  

The Content-Type header should be set to 'application/json' when posting. Server listening for the request should include "Accept=application/json". In Spring MVC you can do it like this:

在发布时,内容类型标头应该设置为“应用/json”。服务器监听请求应该包括“Accept=application/json”。在Spring MVC中,你可以这样做:

@RequestMapping(value="location", method = RequestMethod.POST, headers = "Accept=application/json")

Add headers to the response:

添加标题到响应:

HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");

#22


53  

In Spring you have a defined type: MediaType.APPLICATION_JSON_VALUE which is equivalent to application/json.

在Spring中,您有一个定义类型:MediaType。APPLICATION_JSON_VALUE,它等同于应用程序/json。

#23


49  

The application/json works great in PHP to store an array or object data.

应用程序/json在PHP中非常有用,可以存储数组或对象数据。

I use this code to put data in JSON on Google Cloud Storage (GCS) which is set publically viewable:

我使用此代码将数据放入谷歌云存储(GCS)中,这是公开可见的:

$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
);

To get back the data is straight forward:

要想取回数据,就直截了当:

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

#24


45  

If the JSON is with padding then it will be application/jsonp. If the JSON is without padding then it will be application/json.

如果JSON带有填充,那么它将是应用程序/jsonp。如果JSON没有填充,那么它将是应用程序/ JSON。

To deal with both, it is a good practice to use: 'application/javascript' without bothering whether it is with padding or without padding.

要处理这两种情况,使用“应用程序/javascript”是一种很好的做法,不需要考虑是否有填充或不填充。

#25


44  

For JSON, I am using:

对于JSON,我使用:

 Content-Type: application/json

This is described in the IETF's JSON Data Interchange Format 7158 proposal, Section 1.2: Specifications of JSON.

这是在IETF的JSON数据交换格式7158提案中描述的,第1.2节:JSON的规范。

#26


38  

PHP developers use this:

PHP开发人员使用:

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

    // Do something here...
?>

#27


37  

If you get data from REST API in JSON so you have to use content-type

如果您从JSON中获取REST API的数据,那么您必须使用内容类型。

For JSON data: Content-Type:application/json
For HTML data: Content-Type:text/html,
For XHTML data: Content-Type:application/xhtml+xml,
For XML data: Content-Type:text/xml, application/xml

#28


37  

Extending the accepted responses, when you are using JSON in a REST context...

当您在REST上下文中使用JSON时,扩展所接受的响应。

There is a strong argument about using application/x-resource+json and application/x-collection+json when you are representing REST resources and collections.

当您代表REST资源和集合时,有一个关于使用应用程序/x-resource+json和应用/x-collection+json的激烈争论。

And if you decide to follow the jsonapi specification, you should use of application/vnd.api+json, as it is documented.

如果您决定遵循jsonapi规范,则应该使用应用程序/vnd。api+json,因为它是有文档记录的。

Altough there is not an universal standard, it is clear that the added semantic to the resources being transfered justify a more explicit Content-Type than just application/json.

Altough没有一个通用的标准,很明显,被转移的资源的附加语义比应用程序/json更明确的内容类型。

Following this reasoning, other contexts could justify a more specific Content-Type.

在此推理之后,其他上下文可以为更具体的内容类型辩护。

#29


22  

JSON (JavaScript Object Notation) and JSONP ("JSON with padding") formats seems to be very similar and therefor it might be very confusing which MIME type they should be using. Even though the formats seems to be very similar, there are some subtle differences between them.

JSON (JavaScript对象表示法)和JSONP(“带填充的JSON”)格式似乎非常相似,因此可能会很容易混淆它们应该使用的MIME类型。尽管格式看起来很相似,但它们之间还是有一些细微的差别。

So whenever in any doubts, I have very simple approach (which works perfectly find in most cases), namely, go and check corresponding RFC document.

因此,在任何疑问时,我都有非常简单的方法(在大多数情况下都能很好地找到),即去检查相应的RFC文档。

JSON RFC 4627 (The application/json Media Type for JavaScript Object Notation (JSON)) is a specifications of JSON format. It says in section 6, that the MIME media type for JSON text is

JSON RFC 4627 (JavaScript对象表示法(JSON)的应用程序/ JSON媒体类型)是JSON格式的规范。在第6节中,JSON文本的MIME媒体类型是。

application/json.

JSONP JSONP ("JSON with padding") is handled different way than JSON, in a browser. JSONP is treated as a regular JavaScript script and therefor it should use application/javascript, the current official MIME type for JavaScript. In many cases, however, text/javascript MIME type will work fine too.

JSONP JSONP(“带填充的JSON”)在浏览器中处理的方式与JSON不同。JSONP被视为常规JavaScript脚本,因此它应该使用应用程序/ JavaScript,这是JavaScript的当前官方MIME类型。但是,在许多情况下,文本/javascript MIME类型也可以正常工作。

Note that text/javascript has been marked as obsolete by RFC 4329 (Scripting Media Types) document and it is recommended to use application/javascript type instead. However, due to legacy reasons, text/javascript is still widely used and it has cross-browser support (which is not always a case with application/javascript MIME type, especially with older browsers).

注意,文本/javascript已经被RFC 4329(脚本媒体类型)文档标记为过时,建议使用应用程序/javascript类型。然而,由于遗留的原因,文本/javascript仍然被广泛使用,并且它具有跨浏览器的支持(这并不总是适用于应用程序/javascript MIME类型,特别是对于老的浏览器)。

#30


11  

Content-type: application/json - json
Content-Type: application/javascript - json-P
Content-type: application/x-javascript - javascript
Content-type: text/javascript - javascript BUT obsolete, older IE versions used to use as html attribute.
Content-type: text/x-javascript - JavaScript Media Types BUT obsolete
Content-type: text/x-json - json before application/json got officially registered.

内容类型:应用程序/json - json内容类型:应用程序/javascript - json- p内容类型:应用程序/x-javascript -javascript内容类型:文本/javascript -javascript,但过时的IE版本用作html属性。内容类型:文本/x-javascript - JavaScript媒体类型,但过时的内容类型:在应用程序/json正式注册之前的文本/x-json -json。

#1


8968  

For JSON text:

JSON文本:

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

JSON文本的MIME媒体类型是应用程序/ JSON。默认编码是UTF-8。(来源:RFC 4627)。

For JSONP with callback:

对JSONP回调:

application/javascript

应用程序/ javascript

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

以下是一些在评论中提到的博客文章。

#2


1435  

IANA has registered the official MIME Type for JSON as application/json.

IANA已经注册了JSON作为应用程序/ JSON的正式MIME类型。

When asked about why not text/json, Crockford seems to have said JSON is not really JavaScript nor text and also IANA was more likely to hand out application/* than text/*.

当被问及为什么不使用文本/json时,Crockford似乎说json不是真正的JavaScript或文本,而且IANA更可能分发应用程序/*而不是文本/*。

More resources:

更多资源:

#3


766  

For JSON:

JSON:

Content-Type: application/json

For JSON-P:

JSON-P:

Content-Type: application/javascript

#4


570  

Of course, the correct MIME media type for JSON is application/json, but it's necessary to realize what type of data is expected in your application.

当然,对于JSON来说,正确的MIME媒体类型是应用程序/ JSON,但有必要了解应用程序中期望的数据类型。

For example, I use Ext GWT and the server response must go as text/html but contains JSON data.

例如,我使用Ext GWT,而服务器响应必须作为文本/html,但包含JSON数据。

Client side, Ext GWT form listener

客户端,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");
    }
});

In case of using application/json response type, the browser suggests me to save the file.

在使用应用程序/json响应类型时,浏览器建议我保存文件。

Server side source code snippet using Spring MVC

使用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);
    }
};

#5


423  

JSON:

Response is dynamically generated data, according to the query parameters passed in the URL.

响应是动态生成的数据,根据在URL中传递的查询参数。

Example:

例子:

{ "Name": "Foo", "Id": 1234, "Rank": 7 }

Content-Type: application/json

内容类型:application / json


JSON-P:

JSON with padding. Response is JSON data, with a function call wrapped around it.

JSON和填充。响应是JSON数据,它包含一个函数调用。

Example:

例子:

functionCall({"Name": "Foo", "Id": 1234, "Rank": 7});

Content-Type: application/javascript

内容类型:应用程序/ javascript

#6


366  

If you are using Ubuntu or Debian and you serve .json files through Apache, you might want to serve the files with the correct content type. I am doing this primarily because I want to use the Firefox extension JSONView

如果您正在使用Ubuntu或Debian,并且通过Apache提供.json文件,那么您可能希望使用正确的内容类型来服务文件。我这样做主要是因为我想使用Firefox扩展JSONView。

The Apache module mod_mime will help to do this easily. However, with Ubuntu you need to edit the file /etc/mime.types and add the line

Apache模块mod_mime将帮助您轻松完成这一任务。但是,使用Ubuntu,您需要编辑文件/etc/ mime。键入并添加该行。

application/json json

Then restart Apache:

然后重新启动Apache:

sudo service apache2 restart

#7


354  

If you're calling ASP.NET Web Services from the client-side you have to use application/json for it to work. I believe this is the same for the jQuery and Ext frameworks.

如果你调用ASP。来自客户端的网络Web服务必须使用应用程序/json才能工作。我相信这对于jQuery和Ext框架是一样的。

#8


283  

The right content type for JSON is application/json UNLESS you're using JSONP, also known as JSON with Padding, which is actually JavaScript and so the right content type would be application/javascript.

JSON的正确内容类型是应用程序/ JSON,除非您使用的是JSONP,也就是JSON和填充,这实际上是JavaScript,所以正确的内容类型是应用程序/ JavaScript。

#9


271  

There is no doubt that application/json is the best MIME type for a JSON response.

毫无疑问,应用程序/json是json响应的最佳MIME类型。

But I had some experience where I had to use application/x-javascript because of some compression issues. My hosting environment is shared hosting with GoDaddy. They do not allow me to change server configurations. I had added the following code to my web.config file for compressing responses.

但是我有一些经验,因为一些压缩问题,我不得不使用应用程序/x-javascript。我的托管环境与GoDaddy共享托管。它们不允许我更改服务器配置。我在我的网站上添加了以下代码。压缩响应的配置文件。

<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. I added

通过使用这个,.aspx页被压缩了g-zip,但是JSON响应没有。我添加了

<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. So I personally recommend to use

现在我发现我的JSON响应是用g-zip压缩的。所以我个人推荐使用。

application/x-javascript

only if you want to compress your JSON responses on a shared hosting environment. Because in shared hosting, they do not allow you to change IIS configurations.

只有在共享的宿主环境中压缩JSON响应时才可以。因为在共享主机中,它们不允许您更改IIS配置。

#10


249  

Only when using application/json as the MIME type I have the following (as of November 2011 with the most recent versions of Chrome, Firefox with Firebug):

只有在使用应用程序/json作为MIME类型时,我才有如下(截至2011年11月的最新版本的Chrome, Firefox和Firebug):

  • No more warnings from Chrome when the JSON is loaded from the server.
  • 当从服务器加载JSON时,不再有来自Chrome的警告。
  • Firebug will add a tab to the response showing you the JSON data formatted. If the MIME type is different, it will just show up as 'Response content'.
  • Firebug将添加一个选项卡,以显示格式化的JSON数据。如果MIME类型不同,它将显示为“响应内容”。

#11


224  

Not everything works for content type application/json.

并不是所有内容都适合于内容类型应用程序/json。

If you are using Ext JS form submit to upload file, be aware that the server response is parsed by the browser to create the document for the <iframe>.

如果您使用Ext JS表单提交上传文件,请注意服务器响应被浏览器解析以创建

If the server is using JSON to send the return object, then the Content-Type header must be set to text/html in order to tell the browser to insert the text unchanged into the document body.

如果服务器使用JSON发送返回对象,则必须将Content-Type报头设置为text/html,以便告诉浏览器将文本插入到文档主体中。

See the Ext JS 3.4.0 API documentation.

参见Ext JS 3.4.0 API文档。

#12


211  

JSON is a domain-specific language (DSL) and a data format independent of JavaScript, and as such has its own MIME type, 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. Would you transfer XML via text/plain?

JSON是一种特定于领域的语言(DSL)和独立于JavaScript的数据格式,因此它有自己的MIME类型、应用程序/ JSON。对于MIME类型的尊重当然是客户端驱动的,所以文本/plain可能用于传输字节,但是您可能会将对供应商应用程序域的解释推到不必要的应用程序/json中。是否可以通过文本/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类型的选择是对客户的建议,比如如何解释数据——文本/纯文本/文本/HTML(当它不是HTML的时候)就像类型的删除——它的信息不丰富,就像用类型化的语言制作所有类型的对象一样。

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. 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. If clients are that crippled... then I would consider perhaps HTML injection via an Ajax templating service instead.

我所知道的任何浏览器运行时都不会使用JSON文档,并自动将其作为JavaScript可访问对象提供给运行时,而不需要进行干预,但如果您正在与一个残疾客户打交道,那完全是另一回事。但这并不是整个故事——RESTful JSON服务通常没有JavaScript运行时,但它并没有阻止他们使用JSON作为一种可行的数据交换格式。如果客户是残障人士……然后,我将通过Ajax模板服务来考虑HTML注入。

Application/JSON!

Application / JSON !

#13


195  

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内容类型将是应用程序/json,其他的也已经高亮显示了,但是有些客户机处理得不太好,这就是jQuery推荐默认文本/html的原因。

#14


152  

The correct answer is:

正确的答案是:

Content-Type: application/json

#15


151  

As many others have mentioned, application/json is the correct answer.

正如其他许多人提到的,应用程序/json是正确的答案。

But what haven't been explained yet is what the other options you proposed mean.

但还没有解释的是,你提出的其他选择是什么意思。

  • application/x-javascript: Experimental MIME type for JavaScript before application/javascript was made standard.

    应用/x-javascript:在应用程序/ JavaScript之前进行JavaScript的实验MIME类型。

  • text/javascript: Now obsolete. You should use application/javascript when using javascript.

    text / javascript:现在过时了。在使用javascript时,应该使用应用程序/javascript。

  • text/x-javascript: Experimental MIME type for the above situation.

    文本/x-javascript:上述情况的实验MIME类型。

  • text/x-json: Experimental MIME type for JSON before application/json got officially registered.

    文本/x-json:在应用程序/ JSON正式注册之前的JSON实验MIME类型。

All in all, whenever you have any doubts about content types, you should check this link

总之,当您对内容类型有任何疑问时,您应该检查这个链接。

#16


135  

In JSP, you can use this in page directive:

在JSP中,您可以在页面指令中使用它:

<%@ page language="java" contentType="application/json; charset=UTF-8"
    pageEncoding="UTF-8"%>

The correct MIME media type for JSON is application/json. JSP will use it for sending a response to the client.

JSON的正确MIME媒体类型是应用程序/ JSON。JSP将使用它向客户机发送响应。

#17


103  

application/json” is the correct JSON content type.

“应用程序/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())
  }
}

#18


93  

The IANA registration for application/json says

应用程序/json的IANA注册说明。

Applications that use this media type: JSON has been used to exchange data between applications written in all of these programming languages: ActionScript, C, C#, Clojure, ColdFusion, Common Lisp, E, Erlang, Go, Java, JavaScript, Lua, Objective CAML, Perl, PHP, Python, Rebol, Ruby, Scala, and Scheme.

使用这种媒体类型的应用程序:JSON用于在所有这些编程语言编写的应用程序之间交换数据:ActionScript、C、c#、Clojure、ColdFusion、Common Lisp、E、Erlang、Go、Java、JavaScript、Lua、Objective CAML、Perl、PHP、Python、Rebol、Ruby、Scala和Scheme。

You'll notice that IANA.org doesn't list any of these other media types, in fact even application/javascript is now obsolete. So application/json is really the only possible correct answer.

您会注意到,IANA.org没有列出任何其他的媒体类型,事实上,甚至应用程序/javascript现在已经过时了。因此,应用程序/json是唯一可能的正确答案。

Browser support is another thing.

浏览器支持是另一回事。

The most widely supported non-standard media types are text/json or text/javascript. But some big names even use text/plain.

最广泛支持的非标准媒体类型是文本/json或文本/javascript。但是一些大的名字甚至使用文本/普通。

Even more strange is the Content-Type header sent by Flickr, who returns JSON as text/xml. Google uses text/javascript for some of it's ajax apis.

更奇怪的是Flickr发送的Content-Type头文件,它将JSON作为文本/xml返回。谷歌为一些ajax api使用了文本/javascript。

Examples:

例子:

curl -I "https://ajax.googleapis.com/ajax/services/search/video?v=1.0&q=jsonexample"

Output: Content-Type: text/javascript

输出:的content - type:text / javascript

curl -I "https://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&api_key=f82254c1491d894f1204d8408f645a93"

Output: Content-Type: text/xml

输出:的content - type:text / xml

#19


79  

The right MIME type is application/json

正确的MIME类型是应用程序/json。

BUT

I experienced many situations where the browser type or the framework user needed:

我经历了许多情况,其中浏览器类型或框架用户需要:

text/html

application/javascript

#20


67  

I use the below

我使用下面的

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

#21


60  

The Content-Type header should be set to 'application/json' when posting. Server listening for the request should include "Accept=application/json". In Spring MVC you can do it like this:

在发布时,内容类型标头应该设置为“应用/json”。服务器监听请求应该包括“Accept=application/json”。在Spring MVC中,你可以这样做:

@RequestMapping(value="location", method = RequestMethod.POST, headers = "Accept=application/json")

Add headers to the response:

添加标题到响应:

HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");

#22


53  

In Spring you have a defined type: MediaType.APPLICATION_JSON_VALUE which is equivalent to application/json.

在Spring中,您有一个定义类型:MediaType。APPLICATION_JSON_VALUE,它等同于应用程序/json。

#23


49  

The application/json works great in PHP to store an array or object data.

应用程序/json在PHP中非常有用,可以存储数组或对象数据。

I use this code to put data in JSON on Google Cloud Storage (GCS) which is set publically viewable:

我使用此代码将数据放入谷歌云存储(GCS)中,这是公开可见的:

$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
);

To get back the data is straight forward:

要想取回数据,就直截了当:

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

#24


45  

If the JSON is with padding then it will be application/jsonp. If the JSON is without padding then it will be application/json.

如果JSON带有填充,那么它将是应用程序/jsonp。如果JSON没有填充,那么它将是应用程序/ JSON。

To deal with both, it is a good practice to use: 'application/javascript' without bothering whether it is with padding or without padding.

要处理这两种情况,使用“应用程序/javascript”是一种很好的做法,不需要考虑是否有填充或不填充。

#25


44  

For JSON, I am using:

对于JSON,我使用:

 Content-Type: application/json

This is described in the IETF's JSON Data Interchange Format 7158 proposal, Section 1.2: Specifications of JSON.

这是在IETF的JSON数据交换格式7158提案中描述的,第1.2节:JSON的规范。

#26


38  

PHP developers use this:

PHP开发人员使用:

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

    // Do something here...
?>

#27


37  

If you get data from REST API in JSON so you have to use content-type

如果您从JSON中获取REST API的数据,那么您必须使用内容类型。

For JSON data: Content-Type:application/json
For HTML data: Content-Type:text/html,
For XHTML data: Content-Type:application/xhtml+xml,
For XML data: Content-Type:text/xml, application/xml

#28


37  

Extending the accepted responses, when you are using JSON in a REST context...

当您在REST上下文中使用JSON时,扩展所接受的响应。

There is a strong argument about using application/x-resource+json and application/x-collection+json when you are representing REST resources and collections.

当您代表REST资源和集合时,有一个关于使用应用程序/x-resource+json和应用/x-collection+json的激烈争论。

And if you decide to follow the jsonapi specification, you should use of application/vnd.api+json, as it is documented.

如果您决定遵循jsonapi规范,则应该使用应用程序/vnd。api+json,因为它是有文档记录的。

Altough there is not an universal standard, it is clear that the added semantic to the resources being transfered justify a more explicit Content-Type than just application/json.

Altough没有一个通用的标准,很明显,被转移的资源的附加语义比应用程序/json更明确的内容类型。

Following this reasoning, other contexts could justify a more specific Content-Type.

在此推理之后,其他上下文可以为更具体的内容类型辩护。

#29


22  

JSON (JavaScript Object Notation) and JSONP ("JSON with padding") formats seems to be very similar and therefor it might be very confusing which MIME type they should be using. Even though the formats seems to be very similar, there are some subtle differences between them.

JSON (JavaScript对象表示法)和JSONP(“带填充的JSON”)格式似乎非常相似,因此可能会很容易混淆它们应该使用的MIME类型。尽管格式看起来很相似,但它们之间还是有一些细微的差别。

So whenever in any doubts, I have very simple approach (which works perfectly find in most cases), namely, go and check corresponding RFC document.

因此,在任何疑问时,我都有非常简单的方法(在大多数情况下都能很好地找到),即去检查相应的RFC文档。

JSON RFC 4627 (The application/json Media Type for JavaScript Object Notation (JSON)) is a specifications of JSON format. It says in section 6, that the MIME media type for JSON text is

JSON RFC 4627 (JavaScript对象表示法(JSON)的应用程序/ JSON媒体类型)是JSON格式的规范。在第6节中,JSON文本的MIME媒体类型是。

application/json.

JSONP JSONP ("JSON with padding") is handled different way than JSON, in a browser. JSONP is treated as a regular JavaScript script and therefor it should use application/javascript, the current official MIME type for JavaScript. In many cases, however, text/javascript MIME type will work fine too.

JSONP JSONP(“带填充的JSON”)在浏览器中处理的方式与JSON不同。JSONP被视为常规JavaScript脚本,因此它应该使用应用程序/ JavaScript,这是JavaScript的当前官方MIME类型。但是,在许多情况下,文本/javascript MIME类型也可以正常工作。

Note that text/javascript has been marked as obsolete by RFC 4329 (Scripting Media Types) document and it is recommended to use application/javascript type instead. However, due to legacy reasons, text/javascript is still widely used and it has cross-browser support (which is not always a case with application/javascript MIME type, especially with older browsers).

注意,文本/javascript已经被RFC 4329(脚本媒体类型)文档标记为过时,建议使用应用程序/javascript类型。然而,由于遗留的原因,文本/javascript仍然被广泛使用,并且它具有跨浏览器的支持(这并不总是适用于应用程序/javascript MIME类型,特别是对于老的浏览器)。

#30


11  

Content-type: application/json - json
Content-Type: application/javascript - json-P
Content-type: application/x-javascript - javascript
Content-type: text/javascript - javascript BUT obsolete, older IE versions used to use as html attribute.
Content-type: text/x-javascript - JavaScript Media Types BUT obsolete
Content-type: text/x-json - json before application/json got officially registered.

内容类型:应用程序/json - json内容类型:应用程序/javascript - json- p内容类型:应用程序/x-javascript -javascript内容类型:文本/javascript -javascript,但过时的IE版本用作html属性。内容类型:文本/x-javascript - JavaScript媒体类型,但过时的内容类型:在应用程序/json正式注册之前的文本/x-json -json。