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



我试图通过json数组数据,我得到在控制台日志div内我的html,但我的代码不返回任何数据。有什么问题吗?

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>

最新更新