![](/img/trans.png)
[英]How to get JSON data from multiple URLS in ReactJS using search term in searchbox?
[英]How to get JSON data from multiple URLs
我需要從兩個URL中獲取數據並將其提取到單個表中。 我怎樣才能做到這一點? 有人可以幫我怎么做嗎? 提前致謝。
我嘗試過的是這里。 但是它什么也沒顯示。
var url1 = 'http://localhost:8080/WebService/rest/payment/get/payment';
var url2 = 'http://localhost:8080/WebService/rest/orderdetails/get/all';
$(document).ready(function() {
$.when(
$.getJSON(url1),
$.getJSON(url2)).done(function(result1, result2) {
var table = $("#oTable");
$.each(result1, result2, function(i, value) {
table.append('<tr><td>1</td><td class="txt-oflo">' + value.payment + '</td><td>' + value.username + '</td><td class="txt-oflo">' + value.date + '</td><td><span class="text-success">' + value.price + '</span></td><td><a href=""><button class="btn btn-success">Place</button></a> <a href=""><button class="btn btn-danger">Cancel</button></a></td></tr>');
});
});
});
您可以將更新版本的請求與fetch
使用,並使用Promise.all
和await
(本機支持):
const url1 = 'http://localhost:8080/WebService/rest/payment/get/payment';
const url2 = 'http://localhost:8080/WebService/rest/orderdetails/get/all';
const fetchJSON = url => fetch(url).then(response => response.json())
$(document).ready(async () => {
const [result1, result2] = await Promise.all(fetchJSON(url1), fetchJSON(url2));
const results = [...result1, ...result2];
const table = $("#oTable");
results.forEach((value) => (
table.append('<tr><td>1</td><td class="txt-oflo">' + value.payment + '</td><td>' + value.username + '</td><td class="txt-oflo">' + value.date + '</td><td><span class="text-success">' + value.price + '</span></td><td><a href=""><button class="btn btn-success">Place</button></a> <a href=""><button class="btn btn-danger">Cancel</button></a></td></tr>')
));
});
deferred.done()-添加要解析Deferred對象時調用的處理程序。
您看不到響應可能是因為其中一項承諾被拒絕了。 嘗試使用deferred.then()
或分別循環result1和result2
$.each(result1, function(i, value) {
table.append('<tr><td>1</td><td class="txt-oflo">' + value.payment + '</td><td>' + value.username + '</td><td class="txt-oflo">' + value.date + '</td><td><span class="text-success">' + value.price + '</span></td><td><a href=""><button class="btn btn-success">Place</button></a> <a href=""><button class="btn btn-danger">Cancel</button></a></td></tr>');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.