简体   繁体   English

处理多个AJAX调用

[英]Handling Multiple AJAX calls

I have the following code using AJAX to pull JSON data from a few different URLs and I want to store them into separate arrays. 我有以下代码使用AJAX从几个不同的URL中提取JSON数据,我想将它们存储到单独的数组中。 Here is a sample: 这是一个示例:

var var1 = $.ajax({
    url: FEED1,
    dataType: 'jsonp',
    crossDomain: true,
    success: function (data) {
        if (data) {
            ItemAry1 = data.posts;
        }
    }
});
var var2 = $.ajax({
    url: FEED2,
    dataType: 'jsonp',
    crossDomain: true,
    success: function (data) {
        if (data) {
            ItemAry2 = data.posts;
        }
    }
});

In my code I have several of these. 在我的代码中,我有几个。 The issue that each array has the same exact data. 每个数组具有相同精确数据的问题。 Even the FEED1 and FEED2 are urls to different data. 甚至FEED1和FEED2都是不同数据的网址。

make a function! 发挥作用!

var serviceURL = "http://www.example.com";
var itemArrays = {};

function getFeed(category_id){

    return $.ajax({
        url: serviceURL,
        data: {
            "json":"get_category_post",
            "category_id":category_id,
            "count": 25
        },
        dataType: 'jsonp',
        crossDomain: true,
        success: function (data) {
            if (data) {
                itemArrays[i] = data.posts;
            }
        }
    });
}

var feedPromises = [];
for (var i = 0, count = 9; i < count; i++){
    //start the process to get all the feeds and save their ajax promises into an array
    feedPromises.push(getFeed(i));
}

// wait until all the feeds return data to continue
$.when.apply(this, feedPromises)
    .done(function(){
        // when all the data calls are successful you can access the data via
        itemArrays[0];
        itemArrays[1];
        console.log(itemArrays);
    });

Here's what I ended up using thanks to @Patrick Gunderson: 以下是我最终使用的感谢@Patrick Gunderson:

function get_articles(category_id) {
        console.log('running ' + category_id);

        var url_addition = "?json=get_category_posts&category_id=" + (category_id + 1);
        var url_count = "&count=25";
        var serviceURL = "http://www.example.com/" + url_addition + url_count;

        console.log(serviceURL);

        return $.ajax({
            url: serviceURL,
            dataType: 'jsonp',
            crossDomain: true,
            success: function (data) {
                if (data) {
                    itemArrays[category_id] = data.posts;
                }
                else{
                    console.log("Failed category_id: "+ category_id);
                }
            }
        });
    }

    var feedPromises = [];
    for (var i = 0, count = 10; i < count; i++) {
        //start the process to get all the feeds and save their ajax promises into an array
        feedPromises.push(get_articles(i));
    }
    $.when.apply(this, feedPromises)
        .done(function () {
            // when all the data calls are successful you can access the data via
            console.log(itemArrays);
        });

    console.log(feedPromises);

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

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