Question

I need to chain an unknown (but definitely finite) number of ajax requests. I wanted to create a goAjax() function and watch it with $.when and in my .then() block determine whether or not to call goAjax() again... here is my problem, $.when() only seem to catch goAjax() the first time it fires. What can i do?

                 function goAJAX(url) {

                    var jqxhr = $.ajax({ 
                        dataType: "xml",
                        url: url
                    }).fail(function(data) {
                        console.log( "error" );
                        console.log( data );
                    });

                    return jqxhr;
                }


                $.when( goAJAX(url) ).then(function( result ) {

                    var oaiJSON = $.xml2json(result); 
                    var thisBatchOfRecords = oaiJSON.ListRecords.record.length;
                    var resumptionToken = oaiJSON.ListRecords.resumptionToken.text;

                    totalRecords =+ thisBatchOfRecords;

                    if(thisBatchOfRecords > 99) {
                        var url = "https://www.aURL.com?verb=ListRecords&resumptionToken="+resumptionToken;
                        goAJAX(url);
                    }

                    console.log(totalRecords);
                });

I get one result and never a second :(

Any tips would be great!

Was it helpful?

Solution 2

Ok I got it working...

and it was not quite what sambomartin was saying...

Basically I moved the $.when inside of goAjax() and had it watching the jqxhr object. I still call goajax from within go ajax, but it is now waiting to make that call.

Here is the code:

function goAJAX(url) {
    var jqxhr = $.ajax({
        dataType: "xml",
        url: url
    }).fail(function (data) {
        console.log("error");
        console.log(data);
    });

    $.when(jqxhr).then(function (result) {
        var oaiJSON = $.xml2json(result);
        var thisBatchOfRecords = oaiJSON.ListRecords.record.length;
        var resumptionToken = oaiJSON.ListRecords.resumptionToken.text;
        totalRecords += thisBatchOfRecords;

        if (thisBatchOfRecords >= 99) {
            console.log(totalRecords);
            var url = "https://www.someURL.com/oai/request?verb=ListRecords&resumptionToken=" + resumptionToken;
            res.html("working...");
            goAJAX(url);
        } else {
            console.log("finsished");
            res.html(totalRecords);
        }
    });
}

OTHER TIPS

The second call to goAJAX(url); doesn't actually do anything with the result

Handle the data from in the .done(data) - you could do this in the goAjax function and hold variables containing things like record count to determine whether you want to call again.

I'm not entirely sure what you're trying to do, but guessing that if the number of records is >99 recall the url to get more results?

if this is the case you want to create function that does the when then, and call it repeatedly until the count is 0 or data is empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top