Differences between contentType and dataType in jQuery ajax function

I have the following jQuery callback function, and I have doubts about this (I don't know jQuery very well):

$("form.readXmlForm").submit(function() { // Riferimento all'elemento form che ha scatenato il submit var form = $(this); // Variabile che contiene il riferimento al bottone clickato var button = form.children(":first"); $.ajax({ // Viene eseguita la chiamata AJAX type: "POST", // Tipo di richiesta: POST // URL verso quale viene inviata la richiesta url: form.attr("action"), // Dati XML inviati: data: "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><javaBean><foo>bar</foo><fruit>apple</fruit></javaBean>", // Tipo di media type accettabile dalla response: contentType: "application/xml", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); } }); 

As you can see this function, simply execute an AJAX request for server-side parameter setting for this request.

I found that I am sending the request at the URL, that the request is a POST request, and that the data I'm sending is the following line:

"barapple"

I have some difficulties to understand what are the differences between contentType and dataType

I think contentType indicates the data type acceptable for the HTTP response, right?

And dataType? What to say? The type of data I send in an HTTP request?

In this case, is it β€œtext” because I am sending a text string that contains XML?

+85
javascript jquery ajax
Jan 14 '13 at 16:57
source share
2 answers

From the documentation :

contentType (default: 'application / x-www-form-urlencoded; charset = UTF-8')

Type: String

When sending data to the server, use this type of content. By default, "application / x-www-form-urlencoded; charset = UTF-8" is used, which is great for most cases. If you explicitly pass the content type to $ .ajax (), it will always be sent to the server (even if the data is not sent). If no encoding is specified, data will be transferred to the server using the default server encoding; you must decode it accordingly on the server side.

and

dataType (default: Intelligent Guess (xml, json, script or html))

Type: String

The type of data you expect from the server. If none is specified, jQuery will try to output it based on the MIME response type (XML MIME type will give XML, a JavaScript object will be created in 1.4 JSON, a script will be executed in 1.4 script, and everything else will be returned as a string).

They are essentially the opposite of what you thought.

+120
Jan 14 '13 at 16:59
source share

enter image description here

In English:

  • ContentType : When sending data to the server, use this type of content. The default is application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8 , which is great for most cases.
  • Accepts : a type of content sent in the request header that tells the server which response it will receive in response. Depends on DataType .
  • DataType : the type of data you expect from the server. If none are specified, jQuery will try to infer it based on the MIME type of the response. May be text, xml, html, script, json, jsonp .
+51
Aug 15 '15 at 21:42
source share



All Articles