DataType vs accepts - Ajax Request

I am trying to understand the difference between accepts and dataType in an Ajax request. The documentation reads:

Documentation

accepts (default: depends on the type of DataType) Type: PlainObject The type of content sent to the request header, which tells the server which response it will receive.

dataType (default: Intelligent Guess (xml, json, script or html)) Type: String The type of data that you expect from the server.

Basically, is it the same ?, it has the same purpose.

+7
jquery ajax
source share
1 answer

Here, I hope, the exact answer:

The accepts parameter allows you to change the Accept header in the request.

If you change this option, the Accept header in the request will be set to the specified unit. Note that this is not a string, but an object that maps the MIME type of the received responses. Like { text: "text/plain", html: "text/html" } . The Accept header can be used by the server to provide a response in the expected request format, or fail if it cannot provide a response in one of the formats expected by request.

It is very important that, at least in jQuery 1.11.3 (where I tested), these parameters do not seem to work, instead I managed to change the header using the headers : headers: {Accept : "text/json"} parameter headers: {Accept : "text/json"} .

The dataType parameter allows dataType to pre-process the response

If you define dataType , the query response will be pre-processed by jQuery before being available to the succes handler. For example:

If json specified, the response is parsed using jQuery.parseJSON before passing it as an object to the success handler.

If a script specified, $.ajax() will execute the JavaScript received from the server before passing it to the success handler as a string.

Additional examples are in the Data Types section.

If dataType not set, the Content-Type response will determine what preprocessing should be performed for the response. Keep in mind that changing dataType will also change the Accept header. Usually there is no need to change the Accept header yourself.

Example

request.php

 <?php if(strpos($_SERVER["HTTP_ACCEPT"],"text/javascript") === false) exit("I only provide text/javascript responses"); echo "alert('This is my response!')"; 

index.html

 <button id="send">Send</button> <div id="response"></div> <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <script> $(function(){ $("#send").click(function(){ $.ajax({ method: "GET", url: "request.php", dataType: "script" // Change this to "text" and you will see the difference }).done(function(data) { $("#response").text(data); }); }); }); </script> 

If the dataType parameter dataType set to "script" , the Accept header will include "text/javascript" , so the test for request.php will pass. It will return "alert('This is my response!')" And because the dataType parameter dataType set to "script" jQuery will try to execute this as javascript and then pass it as plain text to the success handler.

If you change dataType to "text" , the Accept header will NOT include "text/javascript" , so the request.php test will fail. It will return "I only provide text/javascript responses" and since the dataType parameter dataType set to "text" , jQuery will pass it as plain text to the success handler.

+8
source share

All Articles