Question
The *right* JSON content type?
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/jsonapplication/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, What MIME type if JSON is being returned by a REST API?, but I'd like a slightly more targeted answer.)
Answer
(get 1557 points)
The MIME media type for JSON text is application/json.ANA 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/*
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.
For example, I use Ext GWT and the server response must go as text/html but contains JSON data.
Client side, Ext GWT 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, the browser suggests me to save the 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);
}
};
overall reviews
