【发布时间】:2015-03-25 09:45:51
【问题描述】:
我在这里进行了很多研究,但似乎找不到适合我的东西。我所拥有的是一个应用程序,我试图出去并返回一个公共汽车站的下四个公共汽车到达时间。我正在联系一个在 JSON 文件中返回此数据的 API。我遇到的问题是我可以看到我的请求通过提琴手发出,但我似乎无法将数据放入数组中。下面是我现在正在处理的代码。我正在尝试将返回的数据转换为您可以看到我失败的表格格式。
最终,我希望在用户单击“显示我接下来的 4 辆巴士到达时间”时出现一个弹出窗口,但这是出于测试目的。我希望用户单击调用此函数的按钮,然后打开带有这些值的类似表格。如果您可以在此代码中提供帮助,我也将不胜感激。
JSON 数据:
[{"ARRIVAL":"01:23P","ROUTE":"208","DIR":"E"},
{"ARRIVAL":"01:53P","ROUTE":"208","DIR":"E"},
{"ARRIVAL":"02:23P","ROUTE":"208","DIR":"E"},
{"ARRIVAL":"02:53P","ROUTE":"208","DIR":"E"}]
代码:
<script>
function getTimes(stopNumber) {
var busArrivalAPI = "http://blahblahblah/rtcTimes/" + stopNumber ";
$.getJSON(busArrivalAPI, function(busArrivals) {
var a = [];
for (var i = 0; i < busArrivals.length; i++) {
a[i] = [busArrivals[i].ROUTE, busArrivals[i].ARRIVAL, busArrivals[i].DIR];
document.getElementById("results").createElement("TR");
for (var b = 0; b < 3; b++) {
var x = document.createElement("TH");
var z = a[i][b];
var t = document.createTextNode(z);
x.appendChild(t);
document.getElementById('results').appendChild(x);
};
};
});
</script>
我的 DIV:
<div style="overflow-x:scroll; overflow-y:scroll;" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="title:'Bus Arrival Times', selected:true">
<table id = 'results'>
<tr>
<th>Route</th>
<th>Arrival Time</th>
<th>Direction</th>
</tr>
</table>
</div>
更新:好的,我使用了下面提供的 makeTable 想法,当我编写如下所示的对 json 数据进行硬编码时,它可以工作。但是,当尝试使用 $.getJSON 时,我现在遇到了一些跨域问题,不知道如何让我的 $.getJSON 请求正常工作。关于如何从我的 getJSON 请求中获取数据的任何输入都很棒。
function getTimes(stopNumber) {
// This is the API address I need to hit. Trying to figure out how to incorporate that and remove the function getJSON where I have the data hard coded.
//var busArrivalAPI = "http://-----/rtcTimes/"+ stopNumber + "?jsoncallback=?";
function makeTable(busArrivals) {
// This will remove old values so table will only load current Times
var results = document.getElementById("results");
var rowCount = results.rows.length;
for (var x=rowCount-1; x>0; x--) {
results.deleteRow(x);
}
// This will populate the result table with the correct bus routes/times/direction
busArrivals.forEach(function(busArrival) {
var tr = document.createElement('tr');
var route = document.createElement('td');
route.appendChild(document.createTextNode(busArrival.ROUTE));
var arrival = document.createElement('td');
arrival.appendChild(document.createTextNode(busArrival.ARRIVAL));
var direction = document.createElement('td');
direction.appendChild(document.createTextNode(busArrival.DIR));
tr.appendChild(route);
tr.appendChild(arrival);
tr.appendChild(direction);
document.getElementById('results').appendChild(tr);
});
}
function getJSON(callback) {
var data = [{"ARRIVAL":"05:23P","ROUTE":"201","DIR":"E"},
{"ARRIVAL":"05:54P","ROUTE":"202","DIR":"E"},
{"ARRIVAL":"06:33P","ROUTE":"203","DIR":"E"},
{"ARRIVAL":"07:11P","ROUTE":"204","DIR":"E"}];
callback(data);
}
getJSON(makeTable);
};
【问题讨论】:
-
您的代码似乎格式错误。您的
getTimes函数缺少结束}。您在浏览器控制台中看到任何错误吗? -
如果这是您的代码的副本和过去,那么您的问题之一是您使用
data.length,data[i].ROUTE,...但您的参数是busArrivals -
再问一次,您是否在浏览器控制台中看到任何错误?
-
omg 发布有效代码,人们在您的代码中发现了如此多的错误,以至于您似乎甚至没有尝试自己解决这个问题。如果您知道您发布的内容存在问题,请修复或解释,否则我们应该如何提供好的建议?
-
@Johnny 寻找语法和命名错误所需的时间,不仅是为了帮助其他人——这是一些人抱怨的——而且也会降低你的实际问题得到解决的可能性。你现在可能很幸运,每个人都心情很好,但你也很容易得到反对票。至此,您应该做的最起码的事情是提供没有语法错误的代码。理想情况下,控制台中不会出现其他错误。如果出现错误,您应该在问题中提及该错误,以表明您是该错误的奖励。
标签: javascript arrays json html-table getjson