简体   繁体   中英

Get data from API json multiple url's into html table

i make a script which get data and append it into html table in JavaScript The problem is that i have multiple page url like https://myAPI?param=2&perPage=100& page=1 (page2,page3 etc) and i have pages like this amount 300 pages

How i can get data from all pages of my API?

here my code, the problem is that only small amount of urls work, if i paste more urls, its not work

 var urls = ['https://myAPI?param=2&perPage=100&page=1', 'https://myAPI?param=2&perPage=100&page=2', 'https://myAPI?param=2&perPage=100&page=3', 'https://myAPI?param=2&perPage=100&page=4', 'https://myAPI?param=2&perPage=100&page=5', 'https://myAPI?param=2&perPage=100&page=6', ] $(document).ready(function(){ $.getJSON(urls, function(data){ var product_data = ''; $.each(data.items, function (i, items) { product_data += '<tr>'; product_data += '<td>'+ items.gimaId + '</td>'; product_data += '<td>'+ items.id + '</td>'; product_data += '<td><a target="_blank" href="url/' + items.code + '">' + items.title + '</a> </td>'; product_data += '<td>'+ items.categoryCodes.code + '</br>' + items.categoryCodes.name +'</td>'; product_data += '<td>'+ items.vendorCode + '</td>'; product_data += '<td>'+ items.price.value + '</td>'; product_data += '<td>'+ items.stock.qty + '</td>'; product_data += '<td><a target = "_blank" href="url' + items.catalogImageId + "/" + '">' + items.catalogImageId + '</a></td>'; product_data += '<td>'+ items.description.content + '</td>'; product_data += '<td>'+ items.active + '</td>'; product_data += '<td>'+ items.storageAddress + '</td>'; product_data += '</tr>'; }); $('#showData').append(product_data); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="showData" class="tavle"> <tr> <th>id</th> <th>id</th> <th>id</th> <th>id</th> <th>id</th> <th>id</th> <th>id</th> </tr> </table>

You need to loop over the pages using a for loop. Example:

var pages = 0;
var url = 'https://myAPI?param=2&perPage=100&page=';
for(var i = 0; pages < 300; i++) {
    newUrl = url + i;
}

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