var name = $.post('/echo/json/', {json:JSON.stringify({'name':"Matt Baker"})}); var lastUpdate = $.post('/echo/json/', {json:JSON.stringify({'lastUpdate':"Hello World"})}); $.when(name, lastUpdate) .done(function (nameResponse, lastUpdateResponse) { var name = nameResponse[0].name; var lastUpdate = lastUpdateResponse[0].lastUpdate; $("#render-me").html(name+"'s last update was: "+lastUpdate); }) .fail(function () { $("#error").html("an error occured").show(); }); //Why the arrays in the done handler? //The new done callbacks receive all the data passed to the done callback of each deferred that's being "combined" with $.when. In this case, we get two arrays - one with all the data that would have been sent to "done" callback of the name request, and one for the data returned to the lastUpdate request.
<div id="render-me"></div> <div id="error" style="display:none;"></div>