gpt4 book ai didi

javascript - 我的网页无法通过 JSON 请求运行

转载 作者:行者123 更新时间:2023-11-30 19:58:34 26 4
gpt4 key购买 nike

我试图弄清楚是否有办法将 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>

最佳答案

在操作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>

关于javascript - 我的网页无法通过 JSON 请求运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53650667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com