Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 464
virtualeyes Avatar asked Jun 16 '12 16:06

virtualeyes


People also ask

Why do we need AJAX in jQuery?

With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page! Without jQuery, AJAX coding can be a bit tricky!

Should I use jQuery for AJAX?

So, the real question is: should you use jQuery to perform your Ajax requests? And the answer is: yes, if you're already using jQuery - if not, you can include jQuery or another Ajax-supporting JS library, or you can implement the Ajax functionality in vanilla JS.

What is the purpose of AJAX process?

AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

What is difference between AJAX and jQuery AJAX?

AJAX is a web development technique for making asynchronous calls to the server. jQuery is a JavaScript library for designing and make some web development tasks easy. It makes it possible to run javascript outside of the browser.


1 Answers

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'     } }); 
like image 164
Darin Dimitrov Avatar answered Sep 26 '22 01:09

Darin Dimitrov