简体   繁体   中英

Execute Function after loop within getJSON

I have a getJSON requests that executes and then loops through the returned data. Once the loop completes, I need to run a function. I can not get anything to execute after the loop. The function printEPL() is the function that I need to execute. A simple alert will not even work. Any help will be greatly appreciated!

var order = <? php echo $orderNumber; ?> ;
function ajaxCall() {
    $.getJSON('http://webaddress.com/api2.php?', {
        order: +order
    }, function (data) {
        var names = data;
        for (var i = 0; i <= data.length; i++) {
            var storeName = names[i].store;
            var tote = names[i].tote;
            var barcode = names[i].barcode;
            var address = names[i].address;
            var state = names[i].state;
            var zip = names[i].zip;
            var date = names[i].date;
            var city = names[i].city;

            qz.append('\nN\n');
            qz.append('q609\n');
            qz.append('Q203,26\n');
            qz.append('B50,40,0,1A,3,7,100,B,"' + barcode + '"\n');
            qz.append('A360,40,0,3,1,1,N," ' + date + '"\n');
            qz.append('A50,200,0,3,1,1,N,"Store - ' + storeName + '"\n');
            qz.append('A250,200,0,3,1,1,N,"Tote - ' + tote + '"\n');
            qz.append('A50,230,0,3,1,1,N," ' + address + '"\n');
            qz.append('A50,260,0,3,1,1,N,"' + city + '"\n');
            qz.append('A50,290,0,3,1,1,N,"' + state + '"\n');
            qz.append('A100,290,0,3,1,1,N,"' + zip + '"\n');
            qz.append('\nP1,1\n');
        }

        * * * * This is where I need the
        function run.* * * * * printEPL();
        alert("test");
    });
} //end of ajax call function

The problem was in the for loop. The JSON array had a length of 3. The loop started at 0 and therefore it would loop 4 times. In order to fix the issue, I just subtracted 1 from length of the array and made the loop only loop 3 times instead of 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM