Does $.ajax automatically execute script, if the data returned is javascript?


I use a single JS file to post all my data back to my server, using:

$.ajax({
        url: "/backend/post.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
        contentType: false,       // The content type used when sending data to the server.
        cache: false,             // To unable request pages to be cached
        processData:false,        // To send DOMDocument or non processed data file it is set to false
        success: function(response, status, xhr)   // A function to be called if request succeeds
        {
            var ct = xhr.getResponseHeader("content-type") || "";
            if(ct.indexOf("text/plain") > -1){
                alert(response);
                console.log('text - response');
            }
            if(ct.indexOf("text/javascript") > -1){
                //eval(response);
                console.log('javascript - response');
            }
        }
    });

It goes through a whole load of functions on the server side but eventually gets to this one: output_javascript("alert('item added');");

function output_javascript($script)
{
    header("content-type:text/javascript");
    echo $script;
}

The idea was to have the $.ajax function display text or execute script from the server.

When $.ajax gets the response from output_javascript("alert('item added');"); then it executes the code twice. When I comment out the code to execute in the success function:

$.ajax({
        url: "/backend/post.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
        contentType: false,       // The content type used when sending data to the server.
        cache: false,             // To unable request pages to be cached
        processData:false,        // To send DOMDocument or non processed data file it is set to false
        success: function(response, status, xhr)   // A function to be called if request succeeds
        {

        }
    });

Then it only executes the response once. Making me believe that $.ajax executes the code before returning the script in the response variable.

Is this true, or am I not understanding $.ajax correctly ? If I am misunderstanding the $.ajax function, could someone please tell me how to resolve this problem?

Yes, ajax will execute returned JavaScript code. We can see this in the documentation:

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:

So if you don't specify dataType, jQuery will figure it out from the response. Okay, but what does it do with a "script" value? Further down:

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

And later in the discussion:

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.

All of this is easily found in the documentation by simply searching for the word "JavaScript" on the page.