0

我正在使用for循环在我的 HTML 表中追加多行jquery

for循环(下面的代码)ajax返回的调用响应上运行array of arrays。但无论否,只有 1 行被添加到表中。数组中的元素。

实际的响应对象非常大,所以我附上一个Google Drive 链接以供参考

如何追加表中的所有行?

代码片段(使用 Jquery)

$(document).ready(function() {
            $('#ghsubmitbtn').on('click', function(e) {

                var data = JSON.stringify(JSONObj);

                $.ajax({
                    url: 'https://api-crt.cert.havail.sabre.com/v4.2.0/shop/flights?mode=live&limit=50&offset=1',
                    method: 'POST',
                    contentType: "application/json; charset=utf-8",
                    data: data,
                    headers: {
                        "Authorization": 'Bearer ' + bat
                    },

                    success: function(data) {
                        var BookBtn = '<input type="submit" value="Book" name="book" class="btn btn-primary">';

                        for (var v = 0; v <= 999; v++) {
                            for (var b = 0; b <= 999; b++) {
                                for (var m = 0; m <= 999; m++) {

                                    var row = $('<tr><td>' + data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].SequenceNumber + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureDateTime + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalDateTime + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureAirport.LocationCode + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalAirport.LocationCode + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureDateTime + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalDateTime + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureAirport.LocationCode + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalAirport.LocationCode + '</td><td>' +
                                        data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].OperatingAirline.Code + '</td><td>' +
                                        BookBtn + '</td></tr>');

                                    $('#tblData').append(row);
                                }
                            }
                        }
                    }
                });
            });
4

1 回答 1

1

问题

控制台中未定义 JSON 属性的 JSON 异常

解析度

在 JavaScript 中安全地访问深度嵌套的值

阅读本文以避免将来出现 JSON 异常。

更正

if (data && data.OTA_AirLowFareSearchRS && data.OTA_AirLowFareSearchRS.PricedItineraries && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v] && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b] && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m]) {
    // Build row using nested JSON
}

更正了 JSFiddle


PS 要快速调试,您必须始终检查browser console以查找任何 JSON 异常。几乎 90% 的问题都可以通过显示的错误字符串得到解决。为了理解真正的问题,我做了同样的事情:)

于 2018-09-09T18:31:17.623 回答