我的网页无法通过JSON请求工作

时间:2018-12-06 11:40:55

标签: javascript jquery json html-table

我试图找出是否有一种方法可以将API结果(JSON)获取到html表。我尝试的方法没有解决。 我正在尝试从tfl API获取JSON。

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

public function check_product($subid, $prod_id){
    $sub = Subcategory::where("subid","=",$subid)->first();
    $checker = //// How do I resolve the correct implementation here ? 
    return response()->json($checker->check_product($prod_id));
}

1 个答案:

答案 0 :(得分:1)

在操作JSON之前,请尝试了解JSON结构(ajax响应)。 我对代码段做了一些更改以供参考(工作中)

<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>