python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
How to Handle multiple Async Ajax requests?

How to Handle multiple Async Ajax requests?

Hello everyone, I am going to share the multiple AJAX calls on a single page asynchronously. I think you know that the $.Ajax() is call asynchronously by nature but problem is that multiple(>1000 calls) Async AJAX calls on a single page.  i.e.



$(function () {
    $.ajax({
        type: "GET",
        url: "https://api.github.com/users/anilsingh581",
        success: function (data) { alert(data); }
    });

    $.ajax({
        type: "GET",
        url: "https://api.github.com/users/anilsingh5812",
        success: function (data) { alert(data); }
    });

    $.ajax({
        type: "GET",
        url: "https://api.github.com/users/anilsingh5813",
        success: function (data) { alert(data); }
    });//etc.......
});

But Its display error : err_insufficient_resources when using chrome any hints.

The solution of above problem is :  $.when() method

//The multiple AJAX requests by using $.when()
$.when(
    $.ajax("https://api.github.com/users/anilsingh581"),
    $.ajax("https://api.github.com/users/anilsingh582"),
    $.ajax("https://api.github.com/users/anilsingh583"),
    $.ajax("https://api.github.com/users/anilsingh584")
    )
    .done(function (data1, data2, data3, data4) {
        //All AJAX requests are finished.
        alert(data1)
        alert(data2)
        alert(data3)
        alert(data4)
    });

OR

$.when($.ajax("/pageurl1.aspx"), $.ajax("/pageurl2.aspx"), $.ajax("/pageurl3.aspx")).then(mySuccess, myFailure);

var mySuccess = function (result) {
    console.log(result);
}

var myFailure = function (result) {
    console.log(result);
}

Thank you!
ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions