jQuery.when understanding
Solution 1:
function showData(data1, data2) {
alert(data1[0].max_id);
alert(data2[0].max_id);
}
function method1() {
return $.ajax("http://search.twitter.com/search.json", {
data: {
q: 'ashishnjain'
},
dataType: 'jsonp'
});
}
function method2() {
return $.ajax("http://search.twitter.com/search.json", {
data: {
q: 'ashishnjain'
},
dataType: 'jsonp'
});
}
$.when(method1(), method2()).then(showData);
Here's a working jsFiddle
Solution 2:
The problem is that you're passing showData()
to then()
, not showData
. You should pass a reference to a function to .then()
:
$.when(method1(), method2())
.then(showData);
or
$.when(method1(), method2())
.then(function () {
showData();
});
Edit
I've put together a working demo. Part of the problem (at least in the code fragment you posted) was that there was no callback function named $callback
. Part of the problem was the $
in the callback name '$callback'
.
So, remove the jsonp: '$callback'
ajax option, so that jQuery defaults to a callback function named callback
, and define a function with that name, and it all works.