jQuery ajax accepts vs dataType

Posted on 2014-04-13
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 250 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 250 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

691 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