Javascript 创建多个Ajax XMLHttpRequest()调用
我想通过带有PHP的URL显示Crunchbase API REST API中的初创公司。为了检索json数据,我使用Ajax提供一个异步请求。 第一个功能是ajaxLoadpage,搜索;将执行第一个xmlhttp-XMLHttpRequest以获取permalink-var permalink=json_de.data.items[c].properties.permalink;第二个非异步的xmlhttp2-XMLHttpRequest使用了这个函数。例如,第二个请求将提供有关特定启动var permalink=kickstarter的更多信息。我只想展示2000-01-01 unixtime>=946684800之后成立的初创公司。我当前的代码可以工作,但我希望这两个请求都是异步的 如何创建两个异步请求?还是有更优雅的方式来完成这项任务?目前加载时间非常长 在20家初创公司之间循环大约需要1分钟。能快点吗?谢谢你的帮助Javascript 创建多个Ajax XMLHttpRequest()调用,javascript,php,jquery,ajax,asynchronous,Javascript,Php,Jquery,Ajax,Asynchronous,我想通过带有PHP的URL显示Crunchbase API REST API中的初创公司。为了检索json数据,我使用Ajax提供一个异步请求。 第一个功能是ajaxLoadpage,搜索;将执行第一个xmlhttp-XMLHttpRequest以获取permalink-var permalink=json_de.data.items[c].properties.permalink;第二个非异步的xmlhttp2-XMLHttpRequest使用了这个函数。例如,第二个请求将提供有关特定启动va
function ajaxLoad(page, search) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { <?php
if (isset($_GET['company'])) { ?>
makeCompanyTable(xmlhttp.responseText); <?php
} else { ?>
makeATable(xmlhttp.responseText); <?php
} ?>
}
}
xmlhttp.open("GET", "get_json.php?p=" + page + "&search=" + search + "&company=<?php echo $companyvar; ?>", true);
xmlhttp.send(); }
function makeATable(json) {
var json_de = JSON.parse(json);
var count = Object.keys(json_de.data.items).length
var c = 0;
var val = "";
var col_counter = 1;
while (c != count) {
var xmlhttp2 = new XMLHttpRequest();
var permalink = json_de.data.items[c].properties.permalink;
xmlhttp2.open("GET", "get_org.php?permalink="+ permalink, false);
xmlhttp2.send();
var json_com = JSON.parse(xmlhttp2.responseText); // Ganze info des jeweiligen Startup
var founding_date = json_com.data.properties.founded_on;
var unixtime = Date.parse(founding_date)/1000;
if (unixtime >= 946684800) {
val = val + '<div class="card profile-view"><div class="pv-header"><img src="';
var cln = "";
cln = cln + json_de.data.items[c].properties.profile_image_url;
if (cln == "" || cln.length <= 0) {
val = val + "img/does_not_exist.png";
} else {
val = val + json_de.data.items[c].properties.profile_image_url;
}
val = val + '" class="pv-main" alt=""></div><div class="pv-body"><h2>';
val = val + json_de.data.items[c].properties.name;
cl = json_de.data.items[c].properties.short_description;
val = val + '</h2><small';
val = val + '>';
val = val + json_de.data.items[c].properties.short_description;
val = val + '</small><a href="startup.php?company=' + json_de.data.items[c].properties.permalink + '" class="pv-follow-btn">Anzeigen</a></div></div>';
//alert(val);
document.getElementById("col_"+col_counter).innerHTML += val;
val = "";
col_counter++;
if(col_counter == 7){
col_counter = 1;
}
}
c++;
} }
是的,您可以提出同步请求。但是,由于您希望在循环下执行此操作,因此可能会在循环本身内部的请求之后对使用的代码产生问题。因此,如果您使用相同的代码就更好了试试jQuery,它会让它变得更简单。您有什么具体的原因让它同步吗?没有。我不知道如何使两个请求都异步。这就是我的问题,我的意思是,您已经做了一个异步请求,所以我想知道为什么您不能通过将相关代码放在xmlhttp2.onreadystatechange中来再次应用完全相同的逻辑,就像您以前做的那样。