gpt4 book ai didi

javascript - getJSON 在第 4 个元素后返回 'undefined'

转载 作者:行者123 更新时间:2023-11-28 16:48:47 25 4
gpt4 key购买 nike

我是 js 的初学者,我正在尝试将 JSON 文档读入 html 表中。好吧,表格创建了,并且它几乎正确地填充了表格中的所有内容。然而,在第四次数据输入之后,最后一列全部未定义。

信息是伪造的

enter image description here

这是我的代码...

<html>
<head>
<link rel="stylesheet" href="mapDiv.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div id = "googleMap"></div>
<script>
function myMap() {
var mapProp= {
center:new google.maps.LatLng(36.3385,-88.8503),
zoom:5,
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
</script>

<script src="Redacted due to api key"></script>
<hr>
<div class = "container">
<div class = "table-responsive">
<table class = "table table-bordered table-striped" id = "customerData">
<tr>
<th>Name</th>
<th>Address</th>
<th>CSZ</th>
<th>latLang</th>
<th>Image</th>
</tr>
</table>
</div>
</div>
</body>
</html>

<script>
$(document).ready(function(){
$.getJSON("http://cs1.utm.edu/~bbradley/map1/customers1.json", function(data){
var custData = '';
$.each(data, function(key, value){
custData += '<tr>';
custData += '<td>' +value.name+'</td>';
custData += '<td>' +value.address+'</td>';
custData += '<td>' +value.cityStateZip+'</td>';
custData += '<td>' +value.latLang+'</td>';
custData += '<td>' +value.image+'</td>';
custData += '</tr>';
});
$('#customerData').append(custData);
});
});
</script>

链接到 json

json

最佳答案

JSON 数据中的某些项目没有 image 属性。您应该检查这一点并在适当的位置显示其他内容。

        custData += '<td>' +(value.image || "No image available")+'</td>';

关于javascript - getJSON 在第 4 个元素后返回 'undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60199108/

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