views:

48684

answers:

9

Right I've been messing around with json for some time, just pushing it out as text and it hasn't hurt anybody (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 is right? Or best? I gather that there are security and browser support issues varying between them...

(I know there's a similar question but I'd like a slightly more targeted answer.)

+231  A: 

RFC 4627:

The MIME media type for JSON text is application/json.

Gumbo
Do we know how widely supported application/json is?
Anirvan
“application/json” is the only correct media type. What exactly do you want to know about it’s support?
Gumbo
Browsers do have a bit of a history of not correctly supporting things like "standards"... ;-)
Richard Ev
To add, a little article explaining why not to use something like text/html: http://jibbering.com/blog/?p=514 - it's older, but still interesting. Not sure if the same caveats apply to text/plain etc.
Michael Stum
@Richard Ev: The recommended behavior with internet messages of any kind is to be tolerant with the input but strict with the output. So you should send your JSON data only with *application/json* but allow other types when receiving data that is expected to be JSON data.
Gumbo
+3  A: 

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 jQuery and ExtJS frameworks.

CL4NCY
jQuery seems to work with at least 'application/json' and 'text/plain'... I haven't tried all the others though.
Nathan
+8  A: 

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

For example I use gwt-ext and server response must goes as text/html but contains JSON data

client side, gwt-ext form listener

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, browser suggests me to save file.

Server side sourse code snippet using 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);
    }     
};
Mikhail.Mamaev
+25  A: 

IANA has registered the official mimetype for JSON as "application/json".

See here:

http://www.iana.org/assignments/media-types/application/

More specifically here:

http://www.ietf.org/rfc/rfc4627.txt

Douglas Crockford pointed to this document here:

http://tech.groups.yahoo.com/group/json/message/337

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/*

See: http://bluesmoon.livejournal.com/227190.html

gnrfan
A: 

ExtJS doesn't require application/json and yes, application/json gets in my way when debugging (calling the ajax url directly), and I agree with Mikhail that it wants me to save a file --

so I suppose when I'm not debugging, I'll use application/json just to be more 'correct'? -- but it doesn't appear that anything is particularly sensitive to it -- my php.ini default is set to text/html so i suppose i'm defaulting to that if i don't set it...which appears to work fine

CINCHAPPS
A: 

Shouldn't there be or is there a method to get the content type from the JSON implementation? e.g. json-lib

can't find any? anybody know if there is an api call for that so that i do not hardcode "application/json"?

tx

Dorin
You can get content-type from your http request wrapper. json-lib just operates with text(String) and mustn't get deal with any HTTP-headers
Olexandr
A: 

Hi,

I am getting some response of HTTP request.

Response header is "Content-Type : text/javascript" and some response body.

I want to save this response body in text file as "a.txt".

Can I do it ?

Regards PJ

Priyanshu
This actually looks more like a question than an answer.
mizipzor
A: 

@CINCHAPPS you can debug application/json easylly with:

https://addons.mozilla.org/en-US/firefox/addon/10869/

Iraê
Leave this as a comment on CINCHAPPS' answer, not as its own answer
Oli
A: 

The category of the MIME type is "application". / JSON is a JavaScript technology (JavaScript Standard Object Notation), and JavaScript is an internet scripting technology which is used heavily in modern sites for all kinds of visual effects.

MIME Application/json

Kiatek106