简体   繁体   English

使用循环将多个HTTP GET请求发送到api

[英]Sending multiple HTTP GET requests to api with a loop

I'm looking for a way to send many requests to an api using a different api url each time. 我正在寻找一种方法,每次使用不同的api网址向api发送许多请求。 An example url for my project is: http://api.bandsintown.com/artists/Hippo%20Campus/events.json?lapi_version=2.0&app_id=music_matcher 我的项目的示例网址是: http : //api.bandsintown.com/artists/Hippo%20Campus/events.json?lapi_version=2.0&app_id=music_matcher

I'm using an HTTP request to pull the JSON info into my script and works perfectly...the first time. 我正在使用HTTP请求将JSON信息提取到脚本中,并且效果很好……第一次。 However, I want to be able to call it 50-100 ish times (max) in a loop with different artist names in the url (I'm using the BandsInTown API ). 但是,我希望能够在URL中使用不同艺术家名称的循环中调用它最多50-100 ish次(我正在使用BandsInTown API )。 For some reason, when I try to use a loop to call the http request multiple times, only one output appears and it is unpredictable which element in the order it will be (it's usually the output associated with the first or second element in the array). 出于某种原因,当我尝试使用循环多次调用http请求时,只会出现一个输出,并且无法确定顺序是哪个元素(通常是与数组中第一个或第二个元素相关联的输出)。 This is what my code looks like: 这是我的代码如下所示:

// HTTP GET call to BandsInTown API
function httpGetAsync(theUrl, callback) { //theURL or a path to file
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
    if (httpRequest.readyState == 4 && httpRequest.status == 200) {
        var data = JSON.parse(httpRequest.responseText);
        if (callback) {
            callback(data);
        }                   
    }
    else {
        alert("error loading JSON doc");
    }
};

httpRequest.open('GET', theUrl, true); 
httpRequest.send(null);
}   



//extracts data from api for each artist
function parseEvent(artist) {
var url = "http://api.bandsintown.com/artists/" + artist + "/events.json?lapi_version=2.0&app_id=music_matcher";

httpGetAsync(url, function(data) {
    var numEvents = Object.keys(data).length;

    //var events = [];
    for (var j = 0; j < numEvents; j++) {
        document.write(data[j].venue.name + "-> ");
        document.write("LAT:" + data[j].venue.latitude + " " + "LNG:" + data[j].venue.longitude);
        document.write("ARTIST: " + data[j].artists[0].name);
        document.write("DATE: " + data[j].datetime);
        document.write(" " + j + " "); 
    }
}); 
}

var artists = ["Drake", "Mac Demarco", "Hippo Campus", "STRFKR"];


for (var i = 0; i < artists.length; i++) {
parseEvent(artists[i]);
document.write(" ---NEXT ARTIST--- ");
}

So I can't tell exactly what's going on but things are acting weird with my current code. 因此,我无法确切知道发生了什么,但是在我当前的代码中,事情变得很奇怪。 I don't have a whole lot of javascript and web development experience yet so any help is appreciated! 我还没有大量的javascript和网络开发经验,所以我们将不胜感激! I was preferably looking for a way to implement this with pure javascript. 我最好在寻找一种使用纯JavaScript实现此方法的方法。 I have had trouble figureing out how to handle Node.js and/or JQuery in Eclipse Neon (the IDE I am using) 我很难弄清楚如何在Eclipse Neon(我正在使用的IDE)中处理Node.js和/或JQuery。

You have implemented closure pretty well so clearly this isn't a problem of success callback of one function overwriting response of all others.But now when you look at document.write() it all gets clear, this function first wipes your whole content clean then it writes whatever you told it to .That's why you hardly see anyone use it 您已经很好地实现了闭包,因此很明显,这不是一个函数重写所有其他函数的成功回调的问题。但是现在,当您查看document.write() ,一切都变得很清楚了,此函数首先将您的整个内容清除干净然后它会写入您告诉的内容。这就是为什么您几乎看不到任何人使用它的原因

`document.write('a');`
`document.write('b');`
`document.write('c');` // a and b are gone you would see only 'c'

So after loop gets over you would only see the output of the last call.Though it's mostly random as to which call would finish last it mostly biased towards some particular value due to the the way servers are tuned. 因此,在循环结束之后,您只会看到最后一次调用的输出,尽管关于哪个调用最后完成的情况大部分是随机的,但由于服务器的调整方式,它通常偏向某个特定值。

So better approach is to use some <div> or something and pour your results into it like this one 所以更好的方法是使用一些<div>或类似的东西,然后将结果倒入其中

<div id="op"></div>

and

function parseEvent(artist) {
    var url = "http://api.bandsintown.com/artists/" + artist + "/events.json?lapi_version=2.0&app_id=music_matcher";

    httpGetAsync(url, function(data) {
        var numEvents = Object.keys(data).length;
        var op = document.getElementById('op');
        op.innerHTML = op.innerHTML + "  <br><br> <h2>---NEXT ARTIST---<h2>  <br>";
        //var events = [];
        for (var j = 0; j < numEvents; j++) {
          op.innerHTML = op.innerHTML + "<br>" + data[j].venue.name + "-> ";
          op.innerHTML = op.innerHTML + "<br>" + "LAT:" + data[j].venue.latitude + " " + "LNG:" + data[j].venue.longitude ;
          op.innerHTML = op.innerHTML + "<br>" +"ARTIST: " + data[j].artists[0].name;
          op.innerHTML = op.innerHTML + "<br>" +"DATE: " + data[j].datetime;
          op.innerHTML = op.innerHTML + "<br>" + " " + j + " <br>"; 
        }
    }); 
}

var artists = ["Drake",  "Hippo Campus", "STRFKR","Mac Demarco"];

for (var i = 0; i < artists.length; i++) {
  parseEvent(artists[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM