I'm trying to accomplish the following sequence of operations using JQuery in HTML.
- A list of urls is constructed
- Each of these urls is requested using $.getJSON(url) in parallel
- Wait for all requests to complete or to fail (404 might happen)
- Take the data of all completed JSON-Requests and do something.
I constructed the Java-Script code posted below. It works perfecly except if one of the requests fails due to a 404-error: Then, $.when
does not run because it instantly aborts if a requests fails. Can you somehow override the ajax-requests, so they don't fail but instead return a empty source?
I already read this and this post, but it does not provide a solution where code can be run after all queries have completed.
function fetchData() {
queries = [];
//urls is initialized somewhere else containing all urls
for(var url in urls) {
queries.push($.getJSON(url));
}
$.when.apply(this, queries).done(function() {
console.log("Finished all queries, got "+arguments.length+" results");
//Now arguments is a array containing the results of all requests
}
//function returns instantly, no problem!
}