Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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

660 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