【问题标题】:My web page is not working from a JSON request我的网页无法通过 JSON 请求运行
【发布时间】:2018-12-06 23:42:48
【问题描述】:

我试图弄清楚是否有办法将 API 结果 (JSON) 获取到 html 表。我尝试的方法没有成功。 我正在尝试从 tfl API 获取 JSON。

这是我目前正在处理的代码:

<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<table class="table table-border table-stiped" id="test_table"
<body>


<table>
<script>
$(document).ready(function(){
$.getJSON("https://api.tfl.gov.uk/Journey/JourneyResults/1000012/to/1000172",function(data){
    var test_data = '';
    $.each(data, function(key, value){
      test_data += '<tr>';
      test_data += '<td>'+value.duration+'</td>';
	  test_data += '<td>'+value.arrivalDateTime+'</td>';
	  test_data += '<td>'+value.departureTime+'</td>';
      test_data += '</tr>';
      
    });
    $('#test_table').append(test_data);
  });
  });
</script>
</table>
</body>

【问题讨论】:

  • 请详细说明“没有工作”是什么意思。控制台、黑屏等是否有错误?
  • 嗨游戏玩家。欢迎来到 StackOverflow! .我检查了你代码中的 api url。返回值不一致。有时,它返回一个数组,有时返回一个 JSON 对象。

标签: javascript jquery json html-table


【解决方案1】:

在操作 JSON 之前,请尝试了解 JSON 结构(ajax 响应)。 我在sn-p中做了一些改动供参考(Working)

<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<table class="table table-border table-stiped" id="test_table"
<body>


<table>
<script>
$(document).ready(function(){
$.getJSON("https://api.tfl.gov.uk/Journey/JourneyResults/1000012/to/1000172",function(data){
    var test_data = '';
    $.each(data.journeys, function(key, value){
      test_data += '<tr>';
      test_data += '<td>'+value.duration+'</td>';
	  test_data += '<td>'+value.arrivalDateTime+'</td>';
	  test_data += '<td>'+value.startDateTime+'</td>';
      test_data += '</tr>';
      
    });
    $('#test_table').append(test_data);
  });
  });
</script>
</table>

【讨论】:

  • 为什么答案被否决?请评论以了解我在哪里犯错
  • 这很好,非常感谢你,我没有对 anser 投反对票,但我也不能投票,因为我没有足够的声誉
猜你喜欢
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 2017-09-22
  • 2019-10-17
  • 2016-02-09
相关资源
最近更新 更多