首页 > 解决方案 > 无法在 d3 js 中加载 json 数据

问题描述

我是 D3 JS 的新手。我想从 D3 js 加载 json 数据。在这里,我不确定是否加载了 json。在这里,没有出现消息。我想显示 html 和 json 数据,

我的数据.json


   [
    {"name":"Hira","age":35},
    {"name":"Basu","age":26},
    {"name":"Mukhia","age":30}
]

HTML

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script>
d3.json("./mydata.json", function(data) {
  console.log(data);
});
</script>
</body>
</html> 

希望大家能给点提示。

标签: javascriptd3.js

解决方案


在这里,我有一个解决方案。

d3.json("./mydata.json").then(function(data){
    console.log(data);
});

在新版本中,我们必须指定 then 函数。在 then 函数中,我们必须创建回调函数。


推荐阅读