简体   繁体   中英

How to search paginated results from the Twitch API

I'm trying to search for games by name from the twitch API, I'm able to do so using the following code:

var games = [];

$.ajax({
    url: 'https://api.twitch.tv/helix/games/top',
    headers: {
        'Client-ID':'xxxxxxxxxxxxxxxxxxxxxx',
        'Content-Type':'application/json'
    },
    method: 'GET',
    dataType: 'json',
    success: function(data){
      games.push(data.data)
    }
});

$(document).ajaxComplete(function() {
  var search = "Counter-Strike".toLowerCase();
  var results = games[0].filter(function(data) {
    return data.name.toLowerCase().indexOf(search) > -1;
  });
  console.log(results);
});

The problem is that this only returns 20 results, how can I search through all the results?

The twitch API does provide a patination in the response but I don't know how it would be used to solve my problem.

The link you've added in your question to the Twitch API shows how to query for more results and how to page them.

?first=20&after=20

The following code should return the second page of 20 results, so from 21 to 40.

$.ajax({
    url: 'https://api.twitch.tv/helix/games/top?first=20&after=20',
    headers: {
        'Client-ID':'xxxxxxxxxxxxxxxxxxxxxx',
        'Content-Type':'application/json'
    },
    method: 'GET',
    dataType: 'json',
    success: function(data){
      games.push(data.data)
    }
});

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