我试图通过ajax对数组的各种元素进行检查。但是,我无法正确运行循环。你能帮助我吗?
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');
};
});
});
答案 0 :(得分:0)
每次都要传递整个数组。您需要使用索引参数并仅传递正在循环的当前元素。
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');
};
});
});