将控制台日志中的 json 数据传递给 html

Pass json data in console log to html

我试图将我在控制台日志中获得的 json 数组数据传递到 html 中的 div,但我的代码没有返回任何数据。可能是什么问题?

let fetchRes = fetch("https://avax.dev/data/validators.json"); 
    // fetchRes is the promise to resolve // it by using.then() method 
fetchRes
  .then((res) => res.json())
  .then((d) => { 
    let len = d.validators.length; 
    console.log(len); 
    document.getElementById('len').innerHTML = len;
   })
   .catch(error => {
      console.log(error);
    });

您的代码正在运行,它还显示了 len,我创建了一个 codeSnippet 请看一下

let fetchRes = fetch("https://avax.dev/data/validators.json"); 
//fetchRes is the promise to resolve // it by using.then() method 
fetchRes.then((res) => res.json()).then((d) => { 
  let len = d.validators.length; 
  console.log(len); 
  document.getElementById('len').innerHTML=len;
}).catch(error => {
  console.log(error);
})
<div id="len"></div>