- 一个基本的fetch请求设置
fetch('https://api.github.com/users/Fire-zy/repos')
.then(function(response){
return response.json();
})
.then(function(myJson){
console.log(myJson);
});
控制台得到返回的数据
- 将得到的数据显示出来,我只取了name,其它内容没取
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple example</title>
</head>
<body>
<ul id="repo"></ul>
<script>
fetch('https://api.github.com/users/Fire-zy/repos')
.then(function(response){
return response.json();
})
.then(function(myJson){
let content='';
for(let repo of myJson){
content+=`<li>
<span>
${repo.name}
</span>
</li>`
}
document.querySelector('#repo').innerHTML=content;
console.log(myJson);
});
</script>
</body>
</html>
得到下面这样
最后添加css修饰一下,得到自己想要的样子