简体   繁体   中英

Jquery Ajax - Get API call with dynamic pagination

I make a GET call using Ajax, it works well and has the expected result. But the API I use has a limit of 50, and uses pagination. What I want to do is to get all data before I call myFunction(data) , so that I can send that function everything, currently I only get the first 50 objects.

$.ajax({
  url: "test.html",
  method: 'GET',
  cache: false,
  success: function(data){
    myFunction(data);
  }
});

It's easy to check if the GET has a next page, as it the return the url of the next page, otherwise it's empty.

You need to create recursive function and you can try this with ajax call async:false.

 var totalPagesLength = 5;
    var pagescount=0;
            function recursively_ajax() {
                $.ajax({
                    url: "test.html",
                    method: 'GET',
                    async:false,
                    cache: false,
                    success: function (data) {
                        pagescount++;
                        if (pagescount < totalPagesLength) {
                            recursively_ajax();
                        }
                    }
                });
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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