5

Possible Duplicate:
$.ajax - dataType

I am using jQuery 1.8.2, and for some reason 'application/json' does not work, but 'json' works as dataType to a standard jquery ajax call. Is this a glitch? A version related difference? or is there an established difference between the two?

$(document).ready(function() {
    $.ajax({
        type : "POST",
        url : '<c:url value="/url.htm" >',
        //dataType : "application/json", <-- does not work
        dataType: 'json' // <-- works
        success : function(data) {
            // do something          
        },
        error : function(data) {
            // do something else
        }
    });
});
Community
  • 1
  • 1
Isaac
  • 2,686
  • 3
  • 27
  • 46
  • 1
    Can you post your code? Also, look here: http://stackoverflow.com/questions/477816/the-right-json-content-type – jchapa Dec 06 '12 at 02:11
  • `dataType` has a limited set of (space separated) options, and it's not the mime type. See [manual](http://api.jquery.com/jQuery.ajax/) – Ja͢ck Dec 06 '12 at 02:11

3 Answers3

10

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.

Musa
  • 89,286
  • 16
  • 105
  • 123
  • I think it would be cool to expand on this and explain what $.ajax is doing behind the scenes to let the server know what we expect and what we are sending. – DutGRIFF Jun 21 '15 at 17:48
1

"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"
Sam Dufel
  • 16,546
  • 3
  • 43
  • 49
1

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
jchapa
  • 3,598
  • 2
  • 23
  • 38