【问题标题】:Create multiple Ajax XMLHttpRequest() calls创建多个 Ajax XMLHttpRequest() 调用
【发布时间】:2015-09-29 15:24:49
【问题描述】:

我想通过带有 PHP 的 URL 显示来自 Crunchbase API (REST API) 的初创公司。为了检索 json 数据,我使用 Ajax 来提供异步请求。 第一个函数ajaxLoad(page, search); 将执行第一个xmlhttp - XMLHttpRequest() 以获取第二个xmlhttp2 使用的“永久链接” - var permalink = json_de.data.items[c].properties.permalink; - XMLHttpRequest(),它不是异步的。第二个请求将提供有关特定启动的更多信息(例如var permalink = "kickstarter")。我只想展示 2000-01-01 (unixtime >= 946684800) 之后成立的初创公司。我当前的代码有效,但我希望这两个请求都是异步的。

如何创建两个异步请求?还是有更优雅的方式来执行这项任务?目前加载时间非常长。

循环 20 家初创公司大约需要 1 分钟。可以更快地完成吗?感谢您的帮助。

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 中再次应用完全相同的逻辑,就像你以前做过。

标签: javascript php jquery ajax asynchronous


【解决方案1】:

是的,您可以发出异步请求。但是,由于您想在循环下执行此操作,因此可能会在循环本身内部的请求之后使用的代码产生问题。因此,最好使用相同的代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2021-02-03
    • 2014-02-14
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多