标签云

微信群

扫码加入我们

WeChat QR Code

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/jsonapplication/x-javascripttext/javascripttext/x-javascripttext/x-jsonBut 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.


Related post - “application/json” constant in .NET framework

2019年06月26日34分07秒

Complete list of MIME types

2019年06月26日34分07秒

Can I send file together Json text?

2019年06月26日34分07秒

does it really matter what mime type it has? it's text that gets parsed into an object. wouldn't it be more natural to pass it as text/plain if you want it to be openable in browser without force download, and application/octet-stream if you want it to be automatically downloaded(useful if you're generating it on a webpage intended to be saved locally). Is there really that many people that will need to know the mime type so the downloader can view it with a special json viewer? What am I missing that warrants it a special mime type?

2019年06月26日34分07秒

Internet Explorer sometimes has issues with application/json - blog is offline

2019年06月26日34分07秒

Imagine I have a document written by somebody which contains plain text. Now that plain text just happens to be valid JSON as well. Would I then be wrong to use text/plain as its mime-type? JSON is a SUB-TYPE of text. So I think both should be allowed. The question is which works better in practice. According to comment by codetoshare IE has problems with application/json.But no browser should have problems with text/plain. If text/plain is unsafe then how can I serve text-files from my web-site?

2019年06月26日34分07秒

A lot of stuff got put into the text/* section in the early days that would probably be put into the application/* section these days.

2019年06月26日34分07秒

And that's a big mistake imho. JSON is text. Can be read/written by any text editor. That is valuable to know, but cannot be derived from application/json. I think it would have been great to have everything that can be opened with a text editor under text, specific XML doctypes under text/xml/xhtml etc... It would really have helped in organizing.

2019年06月26日34分07秒

I can open a .jpg in my text editor.

2019年06月26日34分07秒

Rohmer - You "can" open anything in a text editor, but a binary format like JPEG or a Windows .exe or a .zip will contain non-printable characters which can actually break many text editors or cause undesired behavior. Try running cat file.jpg for example. Whereas any xml or json file is 100% printable. So I think Stijn de Witt's point is a valid one, despite the fact that yes, it's too late to change now.

2019年06月25日34分07秒

I tend to agree with Stijn de Witt. JSON is meant to be viewed and edited with a text-editor.

2019年06月26日34分07秒

JSONP is not really JSON though, it's a technique for passing a JavaScript object literal

2019年06月26日34分07秒

server response must go as text/html. This is true for the ExtJS variant as well.

2019年06月26日34分07秒

The definition of JSON is wrong. It does not need to be dynamically generated or respect query parameters. You can serve a static JSON file. Also, the most upvoted answer has a link to the RFC.

2019年06月26日34分07秒

Also JSONP can be json data assigned to a var.

2019年06月26日34分07秒

usually a reload is enough (faster than restart). Also, note that you can now do "sudo service apache2 reload".

2019年06月26日34分07秒

Ubuntu 12.04 has this by default

2019年06月26日34分07秒

jQuery seems to work with at least'application/json' and 'text/plain'... I haven't tried all the others though.

2019年06月26日34分07秒

jQuery is capable of working with content-Type: text/plain, content-Type: application/json, content-Type: application/json; charset=UTF-8,contentType: "application/x-www-form-urlencoded; charset=UTF-8"

2019年06月26日34分07秒

"So I personally recommending to use application/x-javascript" is where this answer becomes misleading. GoDaddy does allow compression of application/json, I leverage it on my shared hosting and I wouldn't suggest using a different content type to enable compression anyway, it's simply wrong. It can be done, but it will still be wrong. Using different content types for browser support is one thing, using different content types for server-side compression is another.

2019年06月26日34分07秒

Tools that don't adhere to standards should be avoided whenever possible; use application/json per spec.

2019年06月25日34分07秒

one.beat.consumer while that is true, it's not specific to ExtJs per se. It's a browser limitation (or rather, perhaps, a "security measure").

2019年06月26日34分07秒

Surely it would be better to use text/plain so it doesn't apply any HTML semantics to non-HTML content? Or don't browsers let you extract a frame's content if it's got no DOM?

2019年06月26日34分07秒

To add further confusion: I'm just debugging a similar case on Samsung Galaxy Beam (Android 2.3) with default browser, and the iframe seems to fire load event for application/javascript, application/x-javascript, text/javascript, text/plain, but NOT firing it for application/json nor text/html. As of today, Android <=2.3 is About 50% of the Android market share.

2019年06月26日34分07秒

When did text/javascript become obsolete? I'm still filling up HTML documents with <script type="text/javascript" ... tags.

2019年06月26日34分07秒

It makes no difference for browsers, really. It's just obsolete for RFC standards: rfc-editor.org/rfc/rfc4329.txt

2019年06月25日34分07秒

Oli you can safely drop type="text/javascript" and just do <script>...</script> at least according to HTML5.

2019年06月26日34分07秒

Example of such a situation?

2019年06月25日34分07秒

That's Java EE too :: javax.ws.rs.core.MediaType

2019年06月26日34分07秒

The first part of your answer is wrong. "application/jsonp" is not a valid MIME type. The response body of a JSONP is just JavaScript, so one of the MIME-types for JavaScript has to be used.

2019年06月26日34分07秒

application/vnd.api+json seems to be specifically for apis using json:api, a very narrow specification with its own expectations and format, I don't understand it to be for any API that returns json. Please correct me if I'm wrong

2019年06月25日34分07秒