douquanzhan0315 2015-07-10 18:50
浏览 161

创建多个Ajax XMLHttpRequest()调用

I would like to display startups from the Crunchbase API (REST API) via a URL with PHP. To retrieve the json data I am using Ajax to provide a asynchronous request. The first function ajaxLoad(page, search); will do the first xmlhttp - XMLHttpRequest() to get the "permalink" - var permalink = json_de.data.items[c].properties.permalink; that is used by the second xmlhttp2 - XMLHttpRequest() which is NOT asynchronous. The second request will provide further information about the specific startup (var permalink = "kickstarter" for example). I only want to display startups which are founded after 2000-01-01 (unixtime >= 946684800). My current code works but I would like that both request are asynchronous.

How do I create two asynchronous requests? Or is there a more elegant way to carry out this task? Currently the loading time is VERY high.

It takes about 1 minute to loop through 20 startups. Could that be done faster? Thanks for the help.

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++;
} }
  • 写回答

1条回答 默认 最新

  • duanning9110 2015-07-10 19:01
    关注

    Yes, you can make assync request. But, since you want to do it under a loop it might create problems to the code used after the request inside the loop itself. So, it is better if you go with the same code

    评论

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥15 怎么实现输入一个要删除的数后删除后显示剩余数再输入再删除显示剩余数(语言-c语言)
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection