[英]How to Display Api Data in HTML?
<script>
function fetchdata() {
$.get("http://10.10.35.138:5000/data", function (data) { //The link of this line is my api link
$("#visitor").html('Visitor Count : ' + data.people);
$("#time").html('Time : ' + data.time);
});
}
</script>
****HTML PART****
<div class="details">
<p id="visitor">Person Count:</p>
<p id="time">Time:</p>`enter code here`
</div>
My Api data is consist of 2 things count and time.我的 Api 数据由计数和时间两部分组成。 I have mentioned two paragraphs above: I want to display count in first paragraph tag.
我在上面提到了两段:我想在第一段标签中显示计数。 And I want to display Time is another paragraph.
我想显示时间是另一段。 I try more time but API data will not appear paragraph tags.
我尝试更多时间但 API 数据不会出现段落标签。 Plz help me
请帮帮我
You need a loop for making iteration in the data fetched from api provider您需要一个循环来对从 api 提供程序获取的数据进行迭代
$.each(data, function( index, value ) {
});
For each iteration we need to add the value to html:对于每次迭代,我们需要将值添加到 html:
$("#details").append('<p id="visitor"> ' + value. people + '</p> <p id="time">' + value.time + '</p>' );
$.each(data, function( index, value ) {
$("#details").append('<p id="visitor">' + value. people + '</p> <p id="time">' + value.time + '</p>' );
});
final code will be:最终代码将是:
<script>
function fetchdata() {
$.get("http://10.10.35.138:5000/data", function (data) { //The link of this line is my api link
$.each(data, function( index, value ) {
$("#details").append('<p id="visitor">' + value. people + '</p> <p id="time">' + value.time + '</p>' );
});
});
}
</script>
****HTML PART****
<div class="details">
<p id="visitor">Person Count:</p>
<p id="time">Time:</p>`enter code here`
</div>
also if your api data consist just one record of value your code will be:此外,如果您的 api 数据仅包含一条价值记录,则您的代码将是:
function fetchdata() {
$.get("http://10.10.35.138:5000/data", function (data) { //The link of this line is my api link
$("#visitor").html(data.people);
$("#time").html(data.time);
});
}
****HTML PART****
<div class="details">
<p id="visitor">Person Count:</p>
<p id="time">Time:</p>`enter code here`
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.