What is the point of jQuery ajax accepts attrib? Does it actually do anything?
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'
}
});
virtualeyes
Psychologist turned programmer, developing on the JVM with Scala, Akka, Play, and Slick, along with Scala.js in the browser. Currently hacking on the U.S. Hockey Report, an amateur hockey scouting news service.
Updated on July 08, 2020Comments
-
virtualeyes almost 4 years
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 about 12 years+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...
-
Jonas N almost 12 yearsI 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 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.
-
Ciro Santilli OurBigBook.com almost 10 yearsIf neither
dataType
noraccepts
are specified, what HTTP Accepts header is sent? None?