What is the point of jQuery ajax accepts attrib? Does it actually do anything?

virtualeyes picture virtualeyes · Jun 16, 2012 · Viewed 24.3k times · Source

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?

Answer

Darin Dimitrov picture Darin Dimitrov · Jun 16, 2012

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'
    }
});