[英]Javascript Fetch API not showing results on multiple cards
I have a script that fetches the data from a link and then inserts that in tables and shows it to the user.我有一个脚本,它从链接中获取数据,然后将其插入表中并将其显示给用户。 the script worked fine.
脚本运行良好。 but then I decided to make the function more dynamic so I added 2 arguments name and Html div name where the data will display.
但后来我决定让函数更加动态,所以我添加了 2 个参数名称和 Html div 名称,数据将在其中显示。 it worked fine when I called the function the first time but it doesn't show any data on the second call.
当我第一次调用该函数时,它运行良好,但在第二次调用时没有显示任何数据。 I'm using PHP for my projects and new to javascript.
我在我的项目中使用 PHP,并且是 javascript 新手。 but I made a similar function in PHP but that works.
但我在 PHP 中做了一个类似的函数,但有效。 any suggestions?
有什么建议?
function getInfoByCountry(name, displayName) { fetch("https://something.com/countries/" + name) .then(res => res.json()) .then(data => { output = ` <tr> <td>Total Cases</td> <td>${data.cases}</td> </tr> <tr> <td>Today Cases</td> <td>${data.todayCases}</td> </tr> <tr> <td>Deaths</td> <td>${data.deaths}</td> </tr> <tr> <td>Recovered</td> <td>${data.recovered}</td> </tr> <tr> <td>Critical</td> <td>${data.critical}</td> </tr> <tr> <td>Active Cases</td> <td>${data.active}</td> </tr> `; document.getElementById(displayName).innerHTML = output; }); } getInfoByCountry("USA", "USAInfo"); getInfoByCountry("China", "ChinaInfo");
<div class="table-responsive"> <table class="table table-striped mb-0"> <tbody id="ChinaInfo"> </tbody> </table> </div>
Add return
like this像这样添加
return
function getInfoByCountry(name, displayName) {
var display = document.getElementById(displayName);
return fetch("https://smthing.com/countries/"+name)
.then(res => res.json())
.then(data => {
output = `
<tr>
<td>Total Cases</td>
<td>${data.cases}</td>
</tr>
<tr>
<td>Today Cases</td>
<td>${data.todayCases}</td>
</tr>
<tr>
<td>Deaths</td>
<td>${data.deaths}</td>
</tr>
<tr>
<td>Recovered</td>
<td>${data.recovered}</td>
</tr>
<tr>
<td>Critical</td>
<td>${data.critical}</td>
</tr>
<tr>
<td>Active Cases</td>
<td>${data.active}</td>
</tr>
`;
document.getElementById(displayName).innerHTML += output;
}
);
}
getInfoByCountry("USA", "display");
getInfoByCountry("China", "display");
You are adding the result using displayName
as id attribute, you should add another one for USAinfo
as mentioned in the comments, or a better implementation would be to create the table dynamically and append the result to the container.您正在使用
displayName
作为 id 属性添加结果,您应该为USAinfo
添加另一个,如注释中所述,或者更好的实现是动态创建表并将结果附加到容器中。
function getInfoByCountry(name, displayName) { fetch("API_URL" + name) .then(res => res.json()) .then(data => { output = ` <table class="table table-striped mb-0"> <tbody> <tr> <td>Total Cases</td> <td>${data.cases}</td> </tr> <tr> <td>Today Cases</td> <td>${data.todayCases}</td> </tr> <tr> <td>Deaths</td> <td>${data.deaths}</td> </tr> <tr> <td>Recovered</td> <td>${data.recovered}</td> </tr> <tr> <td>Critical</td> <td>${data.critical}</td> </tr> <tr> <td>Active Cases</td> <td>${data.active}</td> </tr> </tbody> </table><hr /> `; document.getElementById("container").innerHTML += output; }); } getInfoByCountry("USA", "USAInfo"); getInfoByCountry("China", "ChinaInfo");
table, td { border: 1px solid; }
<div id="container" class="table-responsive"> </div>
you just need to add return
statement for returning output.您只需要添加
return
语句来返回输出。
here is fiddle you can check function.这是小提琴,您可以检查功能。
function getInfoByCountry(name,displayName) {
debugger;
return fetch("https://jsonplaceholder.typicode.com/posts/"+name)
.then(res => res.json())
.then(data => {
output = `
<tr>
<td>Total Cases</td>
<td>${data.title}</td>
</tr>
<tr>
<td>Today Cases</td>
<td>${data.body}</td>
</tr>
`;
document.getElementById(displayName).innerHTML = output;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.