在getJSON中循环后执行Function

时间:2014-04-27 02:03:54

标签: javascript json function

我有一个执行getJSON请求,然后遍历返回的数据。一旦循环完成,我需要运行一个函数。循环后我无法执行任何操作。 printEPL()函数是我需要执行的函数。简单的警报甚至不起作用。任何帮助将不胜感激!

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

1 个答案:

答案 0 :(得分:0)

问题出在for循环中。 JSON数组的长度为3.循环从0开始,因此循环4次。为了解决这个问题,我只是从数组长度中减去1,并使循环只循环3次而不是4次。