30

Spent a solid hour trying to sort out why on earth this (coffeescript)

$.ajax
  accepts: "application/json; charset=utf-8"

did absolutely nothing to change the accepts header, while this

$.ajax
  dataType: "json"

properly sets the accepts header to application/json; charset=utf-8

Totally confused, am I missing something or is the accepts attrib a year-round April Fool's joke?

virtualeyes
  • 11,147
  • 6
  • 56
  • 91

1 Answers1

30

As always the documentation is your friend:

accepts

Default: depends on DataType

The content type sent in the request header that tells the server what kind of response it will accept in return. If the accepts setting needs modification, it is recommended to do so once in the $.ajaxSetup() method.

dataType

Default: Intelligent Guess (xml, json, script, or html)

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

"xml": Returns a XML document that can be processed via jQuery.

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, _=[TIMESTAMP], to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.

"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

"jsonp": Loads in a JSON block using JSONP. Adds an extra ?callback=? to the end of your URL to specify the callback. Disables caching by appending a query string parameter,
_=[TIMESTAMP], to the URL unless the cache option is set to true.

"text": A plain text string. multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

Now back to your problem. I am not familiar with cofeescript but contrary to dataType which is a string, the accepts parameter is a map and should be used like this:

$.ajax({
    url: ...
    dataType: 'json',
    accepts: {
        xml: 'text/xml',
        text: 'text/plain'
    }
});
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 13
    +1, agreed re: documentation; however, it is entirely non-intuitive to provide an attribute named after the header you are trying to change, that is in fact, a 2nd class citizen relative to the real driver of the bus, dataType. Just confusing as setting both dataType and accepts seems to actually result in default response header. Anyway, am glad to have found that datatype alone does the job... – virtualeyes Jun 16 '12 at 16:53
  • 18
    I must concur with virtualeyes, rather nonintuitive name. And this documentation is like a friend who is difficult to understand quickly :)... Also, the type declaration link 'Map' points to http://api.jquery.com/Types/#Map, which seems to describe a data parameter. There's no example code. And the "Default: depends on DataType" is an understatement, isn't it: it _always_ depends on 'dataType', which gives the order of types, right? ...documentation should ideally be more like a stranger that one understands immediately. Which is of course difficult to achieve, I know. – Jonas N Sep 04 '12 at 17:03
  • If neither `dataType` nor `accepts` are specified, what HTTP Accepts header is sent? None? – Ciro Santilli OurBigBook.com Jul 13 '14 at 10:47