dataType 'application/json' vs. 'json' [duplicate]
Solution 1:
dataType
takes json, it means the request expects a json
response.
contentType
takes application/json
, it means the request is sending json
data
You can send as well as expect json in a request e.g.
$.ajax({
type : "POST",
url : url,
contentType : "application/json",
dataType: 'json',
data: JSON.stringify({some: 'data'}),
success : function(data) {
// do something
},
error : function(data) {
// do something else
}
});
here you're sending json and expecting xml
$.ajax({
type : "POST",
url : url,
contentType : "application/json",
dataType: 'xml',
data: JSON.stringify({xmlfile: 'file.xml'}),
success : function(data) {
// do something
},
error : function(data) {
// do something else
}
});
and here you're sending x-www-form-urlencoded
(jQuery automatically sets this for you), and expect json back
$.ajax({
type : "POST",
url : url,
dataType: 'json',
data: {id: '1'},
success : function(data) {
// do something
},
error : function(data) {
// do something else
}
});
contentType sets the ContentType
HTTP request header, telling the server that the body of this request is of the given type.
dataType sets the Accept
header to tell the server that this is the type of response we want e.g.
Accept:application/json, text/javascript, */*; q=0.01
but regardless of what type of response the server sends jQuery will still attempt to parse it as whatever type you set in the dataType field.
Solution 2:
"application/json" is the correct mime type for json. The jquery dataType
field, however, is expecting one of the following strings:
"xml"
"html"
"script"
"json"
"jsonp"
Solution 3:
Per the json documentation, the correct dataType is "json".
http://api.jquery.com/jQuery.ajax/
Here are the options supported:
- xml
- html
- script
- json
- jsonp
- text