João
João

Reputation: 341

array of deferred ajax requests never entering in when.apply().done()

I have the following code to handle several ajax requests, and wait for them all to proccess and combine their result insite when.apply:

var requestsArray = [];

var url = "http://...";

console.log("url: " + url);

var req1 = $.ajax({
    type: "GET",
    url: url,
    dataType : "xml"
});

req1.done(function (resp1) {
    $(resp1).find('interest').each(function() {

        var interest_id = $(this).find('id').text();
        var interest_name = $(this).find('name').text();

        var request = $.ajax({
              type:"GET",
              URL: "http://en.wikipedia.org/w/api.php?action=parse&format=json&page="+ interest_name + "&redirects&prop=text",
              dataType: "jsonp"
        });
        requestsArray.push(request);

    });

    $.when.apply(null, requestsArray).done(function () {
        console.log(arguments);
         // You can collect the responses in the same order from `arguments`
        var responses = arguments;
    });

});

why it is never entering in the $.when.apply, and it doesnt print any console.log(arguments); ?

Upvotes: 0

Views: 216

Answers (1)

Bergi
Bergi

Reputation: 664650

$.ajax({
    type:"GET",
    URL: "http://en.wikipedia.org/w/api.php?action=parse&format=json&page=" + 
//  ^^^
         interest_name + "&redirects&prop=text",
    dataType: "jsonp"
});

You've misspelled the $.ajax parameter. It needs to be url, not URL.

With no url given, it will fetch the current page, which is not in jsonp format, which will lead to a parseerror, which fill not call your done callback but the fail callback which you didn't pass.

Upvotes: 2

Related Questions