jQuery ajax accepts vs dataType

Posted on 2014-04-13
Medium Priority
Last Modified: 2014-04-15
I understand here that the request accepts xml or plaintext from the server and will try to convert it to json. dataType is per default set to what dataType is set to unless specified otherwise.

    url: ...
    dataType: 'json',
    accepts: {
        xml: 'text/xml',
        text: 'text/plain'

Open in new window

Under what circumstances would I prefer to specify accepts rather then dataType and vice versa?
Question by:itnifl
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 1000 total points
ID: 39998453

Let's look at the documentation.

accepts (default: depends on DataType)
Type: PlainObject
The content type sent in the request header that tells the server what kind of response it will accept in return.

 dataType (default: Intelligent Guess (xml, json, script, or html))
Type: String
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:

As i understand it dataType is a string, so you may define only one of the available choises (as 'xml', json', etc), while accepts may take an array.

So if you want to accept more than one formats, choose accepts. If you want to limit this to only one choose dataType.

This is what i get from the documentation, so anyone feel free to say otherwise.

LVL 35

Accepted Solution

Slick812 earned 1000 total points
ID: 39999294
greetings  itnifl , , You have asked about -
"Under what circumstances would I prefer to specify accepts rather then dataType?"

You must try and understand HOW the AJAX data exchange works, in order to use the correct exchange "formats" (plainText, json, xml) TO and FROM the server. First, ALL data exchanges are a type of TEXT string, and on the SERVER side programming, you must analyze the "Data", the text strings, that you have on server (from database or other) and Need to send back to the browser. If you have several Different "Data" strings to return, you have to set up some sort of Comma Separated Value TEXT string, (json is a CSV text string) or a "tag" based identifier string, like the XML format. The "Format" of the string from the SERVER, will determine the code in browser Javascript, to "Separate" out the combined "several Different Data strings" into different javascript variables, or an Array, or a javascript Object with Key-Value pairs.

If you use the  $.ajax({  setting for accepts: -
accepts: {
   xml: 'text/xml',
   text: 'text/plain'

It may not make any difference at all on the server side return, UNLESS, the server side programming is set to READ these headers and change or modify the text string "Format" sent as the returned text string. It is possible to send back several "Different" string "Formats" (CSV, JSON, XML, HTML) from the server to Javascript, however this requires Very Much experience with AJAX to have it be successful.

For the most part I never use the - accepts: { }  in any $.ajax({   , UNLESS it is a requirement for a SERVER that I do not do the programming for return.

As far as My opinion to you asking - "Under what circumstances would I prefer to specify accepts rather then dataType?"
is that you should not use the  accepts: { } , it will be  a struggle to get correct return if you just set the - dataType:

Author Closing Comment

ID: 40001685
Thanks guys!

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question