Skip to content Skip to sidebar Skip to footer

Problems Making Get Request From Jquery

I'm trying to make an HTTP GET request using the jQuery get() function, but I'm having some trouble. Here's what my code looks like: // get the links on the page var pageLinks =

Solution 1:

Construct bdy after all pages are retrieved, i.e. store get results in a dictionary or array; wait for all gets to finish; then assemble them in the correct order.

Solution 2:

I tried this one and it works:

// get the links on the pagevar pageLinks = $('a');
var bdy 
// loop through each of the links
$(pageLinks).each(function(){
console.log(this);
       // make a GET request to the URL of this link
           $.get($(this).attr("href"), function(data) {

           // concatenate the return string with another
           bdy = bdy + data.toString();
           console.log(bdy);
       });

});

Solution 3:

As an example of what @muratgu has said:

var results = [];
var count = 0;

functionallDone() {
    var bdy = results.join("");
    // do stuff with bdy
}

// get the links on the pagevar pageLinks = $.find('#pageLinks');

// filter the links so we're left with the links we wantvar wantedLinks = $(pageLinks).find('a').filter(function (idx) {
    return $(this).attr('title') !== "Next Page";
});

// remember how many links we're working on
count = wantedLinks.length;

// loop through each of the links
wantedLinks.each(function (idx) {
    // make a GET request to the URL of this link
    $.get($(this).attr("href"), function (data) {
        console.log("here");
        var temp = parse_page(data);
        results[idx] = temp;

        // Decrement the count.
        count--;

        if (count === 0) {
            // All done.allDone();
        }
    });
});

You could go further and abstract this into a data type that can perform N async downloads, and then notify you when all are complete.

Solution 4:

I just found that there are modules that allow one to manage the control flow in JS. The ones I found are:

For help using the above modules, see my follow up question here.

Post a Comment for "Problems Making Get Request From Jquery"