创建一个Javascript数组,我创建并将对象推送到:
checkedAPBoxes.each( function() {
let APTempObj = {};
let apSequence = $(this).data('sequence');
let arDropdownSequenceValue = $('#billingRelaseSelectTiedAR' + $(this).data('sequence')).val();
APTempObj.apSequenceToRelease = apSequence;
APTempObj.relatedARSequence = arDropdownSequenceValue;
APArray.push(APTempObj);
//check if current invoice is comcheck
const url = '/cfc/shipments/ap/ShipmentAPAPI.cfc';
const data = {
method: 'isOriginalComcheckInvoice',
shipmentID: shipmentID,
sequence: apSequence,
returnformat: 'JSON'
};
Edge.Utility.Ajax.get(url, data)
.then(function(result) {
if (result == true){
//add related comcheck invoices to batch
const comcheckURL = '/cfc/shipments/ap/ShipmentAPAPI.cfc';
const comcheckData = {
method: 'getRelatedComcheckInvoices',
shipmentID: shipmentID,
sequence: apSequence,
returnformat: 'json'
};
Edge.Utility.Ajax.get(comcheckURL, comcheckData)
.then(function(resultAPs) {
for (var i = 0; i < resultAPs.length; i++) {
let relatedAPTempObj = {};
relatedAPTempObj.apSequenceToRelease = parseInt(resultAPs[i]);
relatedAPTempObj.relatedARSequence = arDropdownSequenceValue;
APArray.push(relatedAPTempObj);
}
}).catch(function(xhr) {
console.error(xhr);
$uiButton.eui('button', 'complete');
$uiButton.show();
});
}
}).catch(function(xhr) {
console.error(xhr);
$uiButton.eui('button', 'complete');
$uiButton.show();
});
});
但是记录数组会显示长度为5但其中只有3个对象:
基于长度循环数组仅打印前三个元素,即使长度显示为5.
for (var i = 0; i < APArray.length; i++) {
console.log('typeof: ' + typeof(APArray[i]));
console.log(APArray[i]);
}
答案 0 :(得分:0)
ajax.then(回调)和ajax.done(回调)之间存在差异。 &#34;然后&#34;每次收到数据时,函数都会返回一个新的承诺。这就是为什么当收到第一批输出时,即3个对象,它执行回调函数并输出3个对象,并承诺以后再接收2个对象。
所以决议是你使用&#34;完成&#34;功能而不是&#34;然后&#34;。在这种情况下,只有一次回调才能取得圆满成功。在这种情况下,您将收到5个对象。请参阅下面的更新代码。
checkedAPBoxes.each( function() {
let APTempObj = {};
let apSequence = $(this).data('sequence');
let arDropdownSequenceValue = $('#billingRelaseSelectTiedAR' + $(this).data('sequence')).val();
APTempObj.apSequenceToRelease = apSequence;
APTempObj.relatedARSequence = arDropdownSequenceValue;
APArray.push(APTempObj);
//check if current invoice is comcheck
const url = '/cfc/shipments/ap/ShipmentAPAPI.cfc';
const data = {
method: 'isOriginalComcheckInvoice',
shipmentID: shipmentID,
sequence: apSequence,
returnformat: 'JSON'
};
Edge.Utility.Ajax.get(url, data)
.done(function(result) {
if (result == true){
//add related comcheck invoices to batch
const comcheckURL = '/cfc/shipments/ap/ShipmentAPAPI.cfc';
const comcheckData = {
method: 'getRelatedComcheckInvoices',
shipmentID: shipmentID,
sequence: apSequence,
returnformat: 'json'
};
Edge.Utility.Ajax.get(comcheckURL, comcheckData)
.done(function(resultAPs) {
for (var i = 0; i < resultAPs.length; i++) {
let relatedAPTempObj = {};
relatedAPTempObj.apSequenceToRelease = parseInt(resultAPs[i]);
relatedAPTempObj.relatedARSequence = arDropdownSequenceValue;
APArray.push(relatedAPTempObj);
}
}).catch(function(xhr) {
console.error(xhr);
$uiButton.eui('button', 'complete');
$uiButton.show();
});
}
}).catch(function(xhr) {
console.error(xhr);
$uiButton.eui('button', 'complete');
$uiButton.show();