2017-10-11 15 views
0

テストプロジェクト用の公開APIからデータを取得しようとしています。 - 欠落JavaScriptを使用したJsonの解析(ブランクページを表示)

<html> 
<head> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> 
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 
</head> 
<body> 
<script> 

$(document).ready(function() { 
    $.getJSON(http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x', 
    function (json) { 
     var tr; 
     for (var i = 0; i < json.length; i++) { 
      tr = $('<tr/>'); 
      tr.append("<td>" + json[i].userId + "</td>"); 
      tr.append("<td>" + json[i].id + "</td>"); 
      tr.append("<td>" + json[i].title + "</td>"); 
      tr.append("<td>" + json[i].body + "</td>"); 
      $('table').append(tr); 
     } 
    }); 
}); 

</script> 
</body> 
</html> 
+3

が見えます文字列を開かないでください。私は外に出て、javascriptデバッグツールの使用に関するチュートリアルを探します。 –

答えて

1

上記のコメントで述べたように、私はあなたが空白のページを参照してくださいなぜ2つの理由、

  1. を見ることができます。しかし、私は、コードの下に使用していたときに空白のページが表示されます$.getJSON URLでの単一引用符、あなたがアルを追加しているSO

    $.getJSON('http://api....', function(json){...}) 
    
  2. に質問を投稿する前にエラーが発生したとき、ブラウザのコンソールを参照してくださいlのデータを<table>タグに置き換えますが、<table>タグはマークアップにありません。 <table>タグを追加すると、jQueryがデータを追加できるようになります。または、動的に<table>要素をajax呼び出しの中に作成します。あなたがgetJSON関数の構文エラーを持っている
    1:

はここにあなたのコードを持ついくつかの問題があり

<html> 
<head> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> 
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 
</head> 
<body> 

<table></table> 

<script> 

$(document).ready(function() { 
    $.getJSON('http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x', 
    function (json) { 
     var tr; 
     for (var i = 0; i < json.length; i++) { 
      tr = $('<tr/>'); 
      tr.append("<td>" + json[i].userId + "</td>"); 
      tr.append("<td>" + json[i].id + "</td>"); 
      tr.append("<td>" + json[i].title + "</td>"); 
      tr.append("<td>" + json[i].body + "</td>"); 
      $('table').append(tr); 
     } 
    }); 
}); 

</script> 
</body> 
</html> 
1

、変更したコードです。
2.テーブル要素がHTMLで定義されていません。
3.戻ってくるオブジェクトは、「列車」の配列です。だから、ここでjson["Trains"]

で列車を取得する必要があるでしょう、あなたの修正コードは次のとおりです。あなたのことを// API ... ' `注意:構文エラー` $ .getJSON(HTTPを持っているよう

<html> 
<head> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> 
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 
</head> 
<body> 
<table> 
</table> 
<script> 

$(document).ready(function() { 
    $.getJSON("http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x", 
    function (json) { 
     var trains = json["Trains"]; 
     var tr; 
     for (var i = 0; i < trains.length; i++) { 
      tr = $('<tr/>'); 
      tr.append("<td>" + trains[i].Car + "</td>"); 
      tr.append("<td>" + trains[i].Destination + "</td>"); 
      $('table').append(tr); 
     } 
    }); 
}); 

</script> 
</body> 
</html> 
関連する問題