1
function getReportGroups() {
    $.ajax({
        contentType: "application/json; charset=utf-8",
        url: "ReportGroups.ashx",
        data: {
            'method': 'getReportGroups',
            'projectId': '30390'
        },
        dataType: "json",
        success: function (data) {
            alert('inside success');
            var i = 0;
            groupName = [data[i].name];
            while (data[i] != null) {
                alert([data[i].name]);
                alert([data[i].reportGroupId]);
                $("#top-node").append("<li item-checked='true' item-expanded='true'><a href=# style=font-weight:bold>" + [data[i].name] + "</a>");
                i++;
                var id = [data[i].reportGroupId];
                getReports(id);
            }

        },
        error: function (result) {
            alert("Error- Inside the error loop");
        }

    });
}

function getReports(id) {
    $.ajax({
        contentType: "application/json; charset=utf-8",
        url: "ReportGroups.ashx",
        data: {
            'method': 'getReports',
            'reportGroupId': id
        },
        dataType: "json",
        success: function (data) {
            alert('inside getReports success');
            var i = 0;
            groupName = [data[i].name];
            while (data[i] != null) {
                alert([data[i].name]);
                i++;
            }
        },
        error: function (result) {
            alert("Error- Inside the error loop");
        }
    });
}

これは私のコードです。ここで、パラメータ id を使用して getReportGroups() から getReports(id) を呼び出すと、getReoprts() 関数で id がゼロとして渡されます。何が問題なのかわからない。アラート ボックスを使用して、最初の ID に「ID」が存在するかどうかを確認しました。getReportsFunction に有効な ID があります。しかし、私は2番目にIDをゼロとして取得しています。ここで何が間違っていますか?

4

2 に答える 2

0

iを呼び出す前にインクリメントしているように見えますがvar id = data[i].reportGroupId、最初の反復で値をテストすると ( alert([data[i].reportGroupId]);)。

i++while ループの最後のステートメントとして移動し、これで問題が解決するかどうかを確認します。

于 2013-06-12T05:28:04.007 に答える
0

問題は次のようi++に見えます.each()を使用してデータを反復処理できます

$.each(data, function(idx, item){
    alert(item.name);
    alert([item.reportGroupId]);
    $("#top-node").append("<li item-checked='true' item-expanded='true'><a href=# style=font-weight:bold>" + [item.name] + "</a>");
    var id = [item.reportGroupId];
    getReports(id);

})
于 2013-06-12T05:34:54.593 に答える