dongzhui9936 2016-03-07 00:45
浏览 78

循环通过两个JSON数组Ajax

I'm trying to get data from a Json file using the id from a previous previous ajax call looping through the the second array based on the id gotten from the first

I have tried

    $(document).on('click', '.stories', function(e) {
    e.preventDefault();
         var request = $.ajax({
            url: 'includes/functions.php?job=front_title',
            cache: false,
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            type: 'get'
        });


         request.done(function (output) {

        if (output.result === 'success') {     
       var n = output.data[0].title_count;
            $('.blog').empty();
       for (var i=0; i<n; i++) {
            var storytitle = output.data[i].story_view;
            var id = output.data[i].titleID;


            var request2 = $.ajax({
            url: 'includes/functions.php?job=story_episodes',
            cache: false,
            data: 'id=' + id,
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            type: 'get'
            });

            request2.done(function (output2) {

            if (output2.result === 'success') {     
            var n2 = output2.data[0].episode_count;
            for (var i=0; i<n2; i++) {
                var titles =  output2.data[i].title;
                console.log(storytitle + " " + titles);
            }
        }


        else {
                console.log('faileds');
            }

        });

   }

   } else {
                console.log('failed');
            }

        });
});

The storyTitle has a single value and loops through all the titles when i check my console. I tried debugging and found out the second for-loop was only executed once, after executing request2.done, it goes back to the first for-loop after the first has finish all its loop, it executes the second for-loop. I don't know where am missing it.I need help with this.

  • 写回答

1条回答 默认 最新

  • dongtui0650 2016-03-07 14:55
    关注

    Finally solved the problem...Changed my code to...

        $(document).on('click', '.stories', function(e) {
        e.preventDefault();
             var request = $.ajax({
                url: 'includes/functions.php?job=front_title',
                cache: false,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                type: 'get'
            });
    
    
             request.done(function (output) {
    
            if (output.result === 'success') {     
           var n = output.data[0].title_count;
                var jsonArray = $(jQuery.parseJSON(JSON.stringify(output.data))).each(function() {  
             var id = this.titleID;
             var CLASS = this.story_view;
    
    
                var request2 = $.ajax({
                url: 'includes/functions.php?job=story_episodes',
                cache: false,
                data: 'id=' + id,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                type: 'get'
                });
    
                request2.done(function (output2) {
    
                if (output2.result === 'success') {
                    var jsonArray2 = $(jQuery.parseJSON(JSON.stringify(output2.data))).each(function() {  
                    var id2 = this.id;
                    console.log(id + " " + id2);
    
                })
            }
            })
    
       })
       } else {
                    console.log('failed');
                }
    
            });
    })
    

    And it worked fine....thanks to Swapnil Godambe

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化