1252748
1252748

Reputation: 15369

setting returned ajax to variable to done() gives me an object

How come

ajaxFunction().done(function(p){
 console.log(p);
});

returns data as HTML as specified in dataType in the ajaxFunction, but

var data = ajaxFunction().done(function(p){
    console.log(data) 
    //returns what appears to be the entire deferred object
    //to see the returned HTML string, I have to do console.log(data.responseText);
});

What would be the practical application of having that object once I have it stored in data? Can I do something further with it? Or is it not intended to be used like this?

Upvotes: 1

Views: 89

Answers (1)

Alnitak
Alnitak

Reputation: 339965

Your variable data is the promise returned by ajaxFunction().

Having a reference to it allows you to add more .done callbacks, and also .fail callbacks, the latter being handy if your ajaxFunction() has no built-in error handling.

Within your callback functions it would be, umm, unusual to access that variable directly - it's p you're supposed to be using.

Upvotes: 1

Related Questions