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
2
: 19423 At:- 9/20/2017 8:06:20 AM
javascript jQuery Ajax content-type data-type







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

 

2
At:- 9/21/2017 8:40:39 AM Updated at:- 1/17/2023 6:52:27 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

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


profileImage Answered by:- vikas_jk

In Simple words, we can define content type and data type in jQuery Ajax as

Content Type = type of data we are sending to web-service/server, Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases, affects the header

Data type = type of data which we expect from web-service/server, it doesn't affect headers.

1
At:- 12/23/2019 10:28:09 AM


profileImage Answered by:- bhanu

If we simply explain it.

contentType: type of data sending to the server. (default: 'application/x-www-form-urlencoded; charset=UTF-8')

dataType: type of data that you're expecting back from the server. (default: Intelligent Guess (xml, json, script, or html))

Example:

$.ajax({
    type : "POST",
    url : /v1/user,
    dataType : "text",
    contentType: "application/json",
    data : dataAttribute,
    success : function() {

    }
});

That should help.

0
At:- 1/17/2023 9:46:25 AM






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