Solved

jQuery ajax accepts vs dataType

Posted on 2014-04-13
3
4,164 Views
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.

$.ajax({
    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?
0
Comment
Question by:itnifl
3 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 250 total points
Comment Utility
Hi,

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.

Giannis
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 250 total points
Comment Utility
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:
0
 
LVL 2

Author Closing Comment

by:itnifl
Comment Utility
Thanks guys!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.
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.
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now