chovy
chovy

Reputation: 75774

how to get $.ajax settings in response?

I want to know if the original $.ajax() call was made with dataType: 'text'.

Is there anyway to see this in the .done() handler? I was trying $.ajaxSettings() but I do not see an option for dataType.

Upvotes: 2

Views: 1134

Answers (2)

jfriend00
jfriend00

Reputation: 707706

If you are not explicitly setting the context argument for the ajax settings, then the this pointer inside of .done() is the jqXHR object that was created when you started the ajax call (with promise methods added). That object contains any custom settings you used such as dataType. So, you can simply reference the dataType with:

this.dataType

in the .done() handler as in:

$.ajax('example.php', {dataType: "json"}).done(function(data) {
    var type = this.dataType;
});

If you are using the context property which will change this, then you can save the dataType in a local variable before you make the ajax call and access it via that closure:

function yourFunc() {
    var type = "json";
    $.ajax('example.php', {dataType: type, context: someOtherObject}).done(function(data) {
        // can access the local variable type here
    });
}

Upvotes: 3

Yair Nevet
Yair Nevet

Reputation: 13003

What about doing the following:

.done(function(data){ console.log(this.dataType); });

this, in that context, represents the jqXHR object that was created when you started the ajax call.

Upvotes: 0

Related Questions