【发布时间】:2020-10-15 06:56:06
【问题描述】:
我正在尝试建立一个基本的 covid19 网站。我正在使用的代码仅在实际数据中返回 [object Object]。我在这里做错了什么?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("https://api.covid19api.com/summary", function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
});
</script>
</head>
<body>
<button>Get JSON data</button>
<div></div>
</body>
</html>
【问题讨论】:
-
查看
$.each()的文档 -
您无法在 DOM 中显示对象,尝试将
$("div").append(field + " ");替换为console.log(field);您将看到数据是正确的,使用您可以使用的单个项目,例如field.NewConfirmed跨度>
标签: javascript jquery json ajax