0
✓
$.ajax({
url:url
type:"POST",
data:data,
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(){
...
}
})
0
If you're in a client-side environment, investigating about the cross-browser support is mandatory for a well supported web application.
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.
0
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 source 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);
}
};
0
For JSON:
Content-Type: application/json
For JSON-P:
Content-Type: application/javascript
0
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
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
application/json json
Then restart Apache:
sudo service apache2 restart
0
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.
0
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.
0
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):
No more warnings from Chrome when the JSON is loaded from the server.
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'.
0
There is no doubt that application/json is the best MIME type for a JSON response.
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.
<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
<add mimeType="application/json" enabled="true"/>
in the static and dynamic types section. But this does not compressed JSON responses at all.
After that I removed this newly added type and added
<add mimeType="application/x-javascript" enabled="true"/>
in both the static and dynamic types section, and changed the response type in
.ashx (asynchronous handler) to
application/x-javascript
And now I found that my JSON responses were compressed with g-zip. So I personally recommending to use
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.
0
Not everything works for content type application/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>.
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.
See the Ext JS 3.4.0 API documentation.
0
1) JSON :
Definition : Response is Dynamically generated data, according to the Query Parameters passed in the URL.
Eg : { "Name": "Foo", "Id": 1234, "Rank": 7 }
Answer : Content-Type: application/json
2) JSON-P :
Definition : JSON with Padding.
Response is JSON data, with a function call wrapped around it.
Eg : functionCall({"Name": "Foo", "Id": 1234, "Rank": 7});
Answer : Content-Type: application/javascript
0
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
0
JSON is a DSL and a data format independent of JavaScript, and as such has its own mimetype "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 goddamnit!would you transfer XML via "text/plain" ya big troll ya?!
But honestly, assuming its an honest question, 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- its as uninformative as making sll your objects of type Object in a typed language. 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 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.
Application/JSON!
0
Correct Answer:
Content-Type: application/json
0
In JSP, you can use this in page directive:
<%@ 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
0
As many others have mentioned, application/json is the correct answer.
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.
text/javascript: Now obsolete. You should use application/javascript when using javascript.
text/x-javascript: Experimental MIME type for the above situation.
text/x-json: Experimental MIME type for json before application/json got officially registered.
All in all, whenever you have any doubts about content types, you should check this link
0
âapplication/jsonâ is the correct JSON content type.
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())
}
}
0
The right MIME type is application/json
BUT
I experienced many situations where the browser type or the framework user needed:
text/html
application/javascript
0
I use the below
contentType: 'application/json',
data: JSON.stringify(SendData),
0
If the JSON is with padding then it will be application/jsonp. If the JSON is without padding then it will be application/json.
To deal with both, it is a good practice to use: 'application/javascript' without bothering whether it is with padding or without padding.
0
In Spring you have a defined type: MediaType.APPLICATION_JSON_VALUE which is equivalent to application/json.
0
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:
@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");
0
RFC 4627:
The MIME media type for JSON text is application/json.