How do I use a deferred with jQuery’s $.post? I tried:
var myFunc = function(data, textStatus, jqXHR) {
console.log(data);
};
var post = $.post("/url/", someData);
$.when(post).done(myFunc);
The usual
$.post("/url/", someData, function(data) { myFunc(data) });
works fine (after changing the myFunc signature).
$.when... doesn’t work, and no errors show me failures. What exactly is the .done() function passing into myFunc?
The jQuery ajax functions return a jqXHR which is itself a deferred object (it implements the Promise interface). So no need for
$.when().There is also no need to use a named function expression for
myFunc, a normal function declaration is fine.Demo: http://jsfiddle.net/mattball/ng7zT/
This is documented at the jqXHR link above, and also at
$.post.