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
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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
initialise the object angular2 4 35
Enter key on html form page 11 48
Error in JQuery 5 39
Angular - example works but my data is different 13 12
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now