I'm trying to do a check via ajax various elements of an array. However, I am unable to run the loop correctly. can you help me?
https://jsfiddle.net/mzpsLvo6/
var codReferencia = ['1111','2222','3333','4444'];
$.each(codReferencia, function(index, val) {
$.ajax({
url: "https://webapi.toqueacampainha.com.br/api/estoque/produtoprontaentrega?refid=" + codReferencia,
dataType: 'json'
})
.done(function(data) {
if ( data >= '1' ) {
console.log('do anything');
} else {
console.log('do not do anything');
};
});
});
You are passing the entire array each time. You need to use your index argument and only pass the current element you are looping over.
var codReferencia = ['1111','2222','3333','4444'];
$.each(codReferencia, function(index, val) {
$.ajax({
url: "https://webapi.toqueacampainha.com.br/api/estoque/produtoprontaentrega?refid=" + codReferencia[index],
codeReference: codReferencia[index],
dataType: 'json'
})
.done(function(response) {
if ( response >= '1' ) {
console.log('do anything: ' + this.codeReference);
} else {
console.log('do not do anything');
};
});
});