dataType vs accepts – Ajax Request

By | January 12, 2018

I am trying to understand the difference between accepts and dataType in a Ajax Request. Documentation states:


accepts (default: depends on DataType)
Type: PlainObject
The content type sent in the request header that tells the server what kind of response it will accept in return.

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

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


Here is a, hopefully, precise answer:

The accepts option let you change the Accept header in the request

When you change this option, the Accept header in the request will be set to the one(s) specified. Take notice that it is not a string, but an object mapping the MIME type of the accepted responses. Like { text: "text/plain", html: "text/html" }. The Accept header can be used by the server to provide the response in the format expected by the request, or fail in case it can’t provide the response in one of the formats expected by the request.

A really important thing is that, at least in jQuery 1.11.3 (where I tested), this options seems to not be working, instead I managed to change the header using the headers option: headers: {Accept : "text/json"}.

The dataType option let you pre-process the response

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

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

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

More examples here, in the “Data Types” section.

In case that dataType is not set, the Content-Type of the response will determine what pre-processing should be done to the response. Be aware that changing the dataType will change the Accept header too. Usually there is no need to change the Accept header by yourself.



if(strpos($_SERVER["HTTP_ACCEPT"],"text/javascript") === false)
    exit("I only provide text/javascript responses");

echo "alert('This is my response!')";


<button id="send">Send</button>
<div id="response"></div>

<script src=""></script>

            method: "GET",
            url: "request.php",
            dataType: "script" // Change this to "text" and you will see the difference
        }).done(function(data) {


When the dataType is set to "script" the Accept header will include "text/javascript" so the test on request.php will pass. It will return "alert('This is my response!')" and because the dataType is set to "script" jQuery will attempt to execute that as javascript and then pass it as plain text to the success handler.

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

Leave a Reply

Your email address will not be published. Required fields are marked *