What does Content type and data type mean in jQuery ajax request?


I have been working with jQuery for some time now, & while I was working on Ajax GET, iPOST methods, I am not able to understand exactly, what is  content-type and datatype in a POST request, in this example request

$.ajax({
    type : "POST",
    url : /api/connect/user,
    datatype : "application/json",
    contentType: "text/plain",
    success : function(data) {
      //success code here
    },
    error : function(error) {
   //alert error here
},

Is datatype what we are sending or what we expect to get?

I assume Content-type is opposite of that


Asked by:- Sam
1
: 146 At:- 9/20/2017 8:06:20 AM
javascript jQuery Ajax content-type data-type






2 Answers
profileImage Answered by:- Vinnu

contentType: When sending data to the server, use this content type, means you are sending this type of data to server, in this application/json; charset=utf-8 are the common example, whie, application/x-www-form-urlencoded; charset=UTF-8  is the default.

dataType: 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, usually it is json, html or plain text

From the documentation of jQuery ajax, definitions for both the type is as follows

  • contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
    When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding. Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.
  • 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:
    • "xml": Returns a XML document that can be processed via jQuery.
    • "html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
    • "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, _=[TIMESTAMP], to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.
    • "json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)
    • "jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.
    • "text": A plain text string.

 

1
At:- 9/21/2017 8:40:39 AM Updated at:- 9/21/2017 8:40:57 AM
nice explanation 0
By : Sam - at :- 9/23/2017 7:57:05 AM


profileImage Answered by:- manish

contentType When sending data to the server, use this content type.Default is application/x-www-form-urlencoded; charset=UTF-8

dataType 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. Can be text, xml, html, script, json, jsonp

1
At:- 10/9/2017 3:29:49 PM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly