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
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
'application/x-www-form-urlencoded; charset=UTF-8'
)$.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.Intelligent Guess (xml, json, script, or html)
)"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.
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
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.
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly