Newbie here. I have an Ajax function that loops through 3 different requests. However, if the first request fails, I want the other requests to terminate. I tried putting in a 'break' statement but got an 'Illegal break statement' error, I'm guessing since it's not directly inside the for loop, but rather two layers down. I can think of some messy ways to solve this issue, but I'm wondering what a good, simple solution would be...
function runQueries(request) {
var reports = ['103, 187, 190'];
for (i=0; i < reports.length; i++) {
report = reports[i];
$.ajax({
url: 'proxy.php',
data: {requrl: request + '&reportType=' + report},
dataType: 'xml'
})
.done(function(response) {
if ($(response).find('Locations').children('Location').length < 1) {
displayError(response, report);
break;
}
use
instead of break
use i =reports.length+1;
$.ajax({
url: 'proxy.php',
data: {requrl: request + '&reportType=' + report},
dataType: 'xml'
})
.fail(function() {
i =reports.length+1;
}