weixin_33726943 2018-03-26 06:34 采纳率: 0%
浏览 26

进行嵌套的ajax调用

I am trying to make a HN clone with vanilla JS.

JS CODE:

var apiResult = document.getElementById("apiResult");

function apiContent() {
var url_end = ' https://hacker-news.firebaseio.com/v0/topstories.json? 
print=pretty';
var apiCall = new XMLHttpRequest();
var links = [];

apiCall.onload = function() {
    if(this.status == 200) {
        var apiData = JSON.parse(apiCall.responseText);

        for( var i = 0; i < apiData.length; i++){

            var url = 'https://hacker-news.firebaseio.com/v0/item/' ;
            var c_url = apiData[i] + '.json?print=pretty';
            var final_url = url + c_url;
            links.push(final_url);
        }
            // console.log(links);

            for(var j = 0; j < links.length; j++) {
                var a = links[j];
                var secondRequest = new XMLHttpRequest();
                secondRequest.onload =  function() {
                var secondData = JSON.parse(secondRequest.responseText);
                console.log(secondData);
            }
            secondRequest.open('GET', a, true);
            secondRequest.send();
            }       
    }
    else {
        console.log('Unable to fetch data');
    }
}
apiCall.open("GET", url_end, true);
apiCall.send();


}

The first api call 'url_end' gives the ID of the top 500 stories, it just returns the ID and nothing else. So to fetch the content corresponding to the story ID i'm making an another AJAX call but this time the url is updated with the ID but its not working.

How do I make this work??

  • 写回答

1条回答 默认 最新

  • weixin_33744854 2018-03-26 07:21
    关注

    I have created the sample example. You need a closure to call multiple ajax inside the for the loop. Sample JSON hosted on http://jsonstore.online Documents: http://jsonstore.online/#!/documentation.

    WORKING JS CODE:

    var apiResult = document.getElementById("apiResult");
    function apiContent() {
    var url_end = 'https://api.jsonstore.online/api/PsrWFAEwRVWXXU9y/cities';
    var apiCall = new XMLHttpRequest();
    var links = [];
    apiCall.onload = function() {
        if(this.status == 200) {
            var apiData = JSON.parse(apiCall.responseText);
            for( var i = 0; i < apiData.length; i++){
                var final_url = 'https://api.jsonstore.online/api/PsrWFAEwRVWXXU9y/cities?id='+apiData[i].id ;
                links.push(final_url);
            }
            console.log(links); // Print Link Array
            
            for(var j = 0; j < links.length; j++) {
                (function(a){
                  let secondRequest = new XMLHttpRequest();
                  secondRequest.onload =  function() {
                    let secondData = JSON.parse(secondRequest.responseText);
                    console.log(secondData);
                  }
                  secondRequest.open('GET', a, true);
                  secondRequest.send();
               })(links[j]);
            } 
            
        }
        else {
            console.log('Unable to fetch data');
        }
    }
    apiCall.open("GET", url_end, true);
    apiCall.send();
    }
    apiContent();

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型