【发布时间】:2018-05-31 00:27:03
【问题描述】:
我是 XML 和 JSON 的初学者,我无法在我的 HTML 中显示我的数据。我正在尝试从 xml api 中提取数据并将其显示在我的 div 中。
但我的 div 中似乎没有显示任何内容。对不起,如果我的代码有点乱。如果您想查看 XML 文件的样子,可以参考http://api.nea.gov.sg/api/WebAPI/?dataset=24hrs_forecast&keyref=781CF461BB6606AD48001FDD2657FAF0F8C6C64F041BB440。
更新 我现在可以在将数据源更改为 json 后显示我的数据,因为我只能显示 2 行数据,我不知道为什么会这样。
var json4day; //global variables - all functions can read and store values inside
$(function() {
Load4day(); //call function to load data****
});
function Load4day(){
//2 hour code here
$.ajax({
url: "https://api.data.gov.sg/v1/environment/4-day-weather-forecast",
dataType: "json",
headers: {"api-key": "nVjYBrTc4KMLNjJQrKwlc0Be9V5zFYXZ"
}
})
.done(function(json) { //tween max oncomplete
json4day=json;
ShowData(); //load PSI*****
})
.fail(function() {
console.log("error");
});
}
function ShowData(){
console.log("Show data");
console.log(json4day);
var tforecasts=json4day.items[0].forecasts.length;
console.log(tforecasts);
for(var i=0;i<tforecasts;i++){
var fc=json4day.items[0].forecasts[i].forecast;
var date=json4day.items[0].forecasts[i].date;
var icon=json4day.items[0].forecasts[i].relative_humidity;
// console.log(lt);
var html="<div data-index='"+i+"'>"+date+"<br>"+fc+"<br></div>";
var html2="<div data-index='"+i+"'>"+date+"<br>"+fc+"<br></div>";
$(".content1").append(html);
$(".content1").html(html);
$(".content1").append(html2);
$(".content1").html(html2);
}
}
我正在尝试在下面的 div 中显示我的数据
div id="main1">
<div class="b1">
<div class="4hr">
<p class="t1">4 Hour Forecast </p>
<div id="target">
click here
</div>
</div>
<!--<iframe width="600" height="400" src="https://data.gov.sg/dataset/weather-forecast/resource/4df6d890-f23e-47f0-add1-fd6d580447d1/view/91f4e399-5a83-4cab-9491-09464db88661" frameBorder="0"> </iframe>-->
</div>
【问题讨论】:
标签: javascript html css json xml