Ajax调用没有获取数据

Ajax call is not fetching the data

本文关键字:获取 数据 调用 Ajax      更新时间:2023-09-26
$(document).ready(function(){
  /* Union Station */
  $.getJSON("http://myttc.ca/Union_station.json?callback=?",
       function(data){
         if (routes.length > 0) {
            $.each(data.stops, function(i,item){
               $("#Union").append("<p>Stop Name: " + item.name + "</p>");
               $.each(item.routes, function(i,item){
                  $.each(item.stop_times, function(i,item){
                     $("#Union").append("<p>Departure Times: " + item.departure_time + "</p>");
                     $("#Union").append("<p>Shape: " + item.shape + "</p>");
                  });
               });
            });
        }
   });
});     

我得到一个空的屏幕在这个
谁能帮助修复这个jquery从json获取数据我只想显示带有出发时间的停车详细信息

应该是:

function(data){
    if (data.stops.length > 0) {

这是ajax的结果:

Object {time: 1367157909, stops: Array[8], name: "Union Station", uri: "union_station"}
编辑:

我猜你需要一些额外的逻辑,这是对象:

stops: Array[8]
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
  agency: "Toronto Transit Commission"
  name: "Union Station Subway Platform"
  routes: Array[1]
    0: Object
      name: "Yonge-University-Spadina Subway"
      route_group_id: "1"
      stop_times: Array[6]
        0: Object
          departure_time: "10:07a"
          departure_timestamp: 1367158079

你可以这样做:

for (var i = 0, l = data.stop.length, stop; i < l; i++) {
    stop = data.stop[i];
    // If current stop has stop_times then...
    if (stop.stop_times.length) {
        // do something...
        console.log(stop.stop_times);
    }
}

示例:http://jsfiddle.net/fBd3s/

routes可能不是您要查找的:

function(data){
    if (routes.length > 0) {

至少我看不出routes将被设置在哪里。你可能需要

function(data){
    if (data.length > 0) {

成功回调中没有routes,因此删除该条件

$(document).ready(function(){
    /* Union Station */
    $.getJSON("http://myttc.ca/Union_station.json?callback=?",
              function(data){
                  $.each(data.stops, function(i,item){
                      $("#Union").append("<p>Stop Name: " + item.name + "</p>");
                      $.each(item.routes, function(i,item){
                          $.each(item.stop_times, function(i,item){
                              $("#Union").append("<p>Departure Times: " + item.departure_time + "</p>");
                              $("#Union").append("<p>Shape: " + item.shape + "</p>");
                          });
                      });
                  });
              });
});    

演示:小提琴

参考此问题的接受值。这是Jquery特有的。

API文档会给你更多的信息。http://api.jquery.com/jQuery.getJSON/