简体   繁体   English

从 API json 多个 url 中获取数据到 html 表中

[英]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我制作了一个脚本来获取数据并将其附加到 JavaScript 中的 html 表问题是我有多个页面 url,如 https://myAPI?param=2&perPage=100& page=1 (page2,page3 等),我有像这样的页面这个数量 300 页

How i can get data from all pages of my API?如何从我的 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.您需要使用 for 循环遍历页面。 Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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