This has me stumped. I have an array of urls (for data) which I need to pull into the page and process the results once all are loaded. I am attempting to use JQuerys Deffered functionality to ensure that all of the ajax calls have completed before I process the results. As soon as I introduced it (the when().done() functionality), my responseText magically disappeared.
My simplest example:
$.when([$.ajax("pathToData")]).done(
function(results) {
console.log(results[0]); //object with a responseText attribute
console.log(results[0].responseText); //undefined !!!
}
)
I suspect I am missing something simple, but the more I read the documentation the more this looks correct. I’m hoping someone else can spot the issue easily and point me in the right direction. Thanks in advance!
The weird behaviour you’re seeing is a limitation of the
console, and actually nothing to do with your code.The resolution of the properties of the object are deferred until you expand the
Objectin theconsole. By that time the AJAX request has completed andresponseTextis available. However, the value ofresults[0].responseTextis resolved immediately asundefined.If you did:
You’ll see:
Instead.
As to how to solve your problem; I’ve never known
$.when()to accept arrays, nor does the documentation say it does. Because of this,when()seems to be executing thedone()immediately, as the array is not a deferred (per the docs):Instead pass your AJAX request(s) as separate parameters, as shown in the docs:
As such:
There you get:
… and an updated fiddle: http://jsfiddle.net/39mHw/2/