循环时ajax调用的非法中断声明

时间:2017-02-13 20:17:37

标签: javascript jquery ajax for-loop

我试图在for循环中进行ajax调用,然后抛出异常" Uncaught SyntaxError:Illegal break statement"。当调用函数getProviderDetails()时,它发生在最后一行。

我也尝试过调用同步,但它也没有用。甚至改变了回归的循环,因为我在其他答案中读到没有运气。

以下是代码:

    function getProviderDetails(origin,destination,outbound,inbound) {
        $.get("/api/provider.php", {
            async: false,
            origin: origin,
            destination: destination,
            outbound: outbound,
            inbound: inbound
            },
        function(data, status) {
            console.log(data);
            var values = data.split(";");
            if (dates.length < 2) {
                break;
            }
            var price = values[0];
            if (price < 1) {
                break;
            }
            var link = values[1];
            printData(origin,destination,outbound,inbound,price,link);
        });
    }

    function printData(origin,destination,outbound,inbound,price,link) {
        var row = "<tr>"+"<td>"+origin+"</td>"+"<td>"+destination+"</td>"+"<td>"+outbound+"</td>"+"<td>"+inbound+"</td>"+"<td>"+price+"</td>"+"<td>"+link+"</td>"+"</tr>";
        $('#prices').append(row);
    }

    function getMonths(origin,destination) {
        $.get("/months.php", {
            async: false,
            from: origin,
            to: destination,
            country: "AR",
            currency: "ARS"
        },
        function(data, status) {
            console.log(data);
            var counter = 0;
            var months = [];
            for (var key in data) {
                counter++;
                if (counter > 2) {
                    break;
                }
                getDates(origin,destination,key);
            }
        });
    }

    function getDates(origin,destination,month) {
        $.get("/api.php", {
            from: origin,
            to: destination,
            month: month,
            country: "AR",
            currency: "ARS"
            },
        function(data, status) {
            console.log(data);
            var counter = 0;
            for (var key in data) {
                counter++;
                var outboundDates = [];
                var inboundDates = [];
                if (counter > 10) {
                    return;
                }
                var dates = key.split(".");
                if (dates.length < 2) {
                    return;
                }
                var outbound = dates[0];
                var inbound = dates[1];
                //outboundDates.push(outbound);
                //inboundDates.push(inbound);
                //iterateDates(origin,destination,outboundDates,inboundDates,0);
                getProviderDetails(origin,destination,outbound,inbound);                    
            }
        });
    }



    getMonths("BUE","MAD");

0 个答案:

没有答案