美文网首页
19.fetch(请求三种数据格式)

19.fetch(请求三种数据格式)

作者: Night_LION | 来源:发表于2018-06-06 22:35 被阅读0次

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="https://cdn.bootcss.com/skeleton/2.0.4/skeleton.css" rel="stylesheet">
</head>
<body>

<div class="container">
  <h1>Fetch Api sandbox</h1>
  <button id="button1">请求本地文本数据</button>
  <button id="button2">请求本地json数据</button>
  <button id="button3">请求网络接口</button>
  <br><br>
  <div id="output"></div>
</div>

<script src="app.js"></script>
</body>
</html>

JS

document.getElementById('button1').addEventListener('click',getText);
document.getElementById('button2').addEventListener('click',getJson);
document.getElementById('button3').addEventListener('click',getExternal);

// 获取本地纯文本数据
function getText(){
  fetch("test.txt")
      .then((res) => res.text())
      .then(data => {
        console.log(data);
        document.getElementById('output').innerHTML = data;
      })
      .catch(err => console.log(err));
}

// 获取本地json数据
function getJson(){
  fetch("posts.json")
      .then((res) => res.json())
      .then(data => {
        console.log(data);

        let output = '';
        data.forEach((post) => {
          output += `<li>${post.title}</li>`;
        })
        document.getElementById('output').innerHTML = output;
      })
      .catch(err => console.log(err));
}

// 请求网络api
function getExternal(){
  // https://api.github.com/users
  fetch("https://api.github.com/users")
      .then((res) => res.json())
      .then(data => {
        console.log(data);

        let output = '';
        data.forEach((user) => {
          output += `<li>${user.login}</li>`;
        })
        document.getElementById('output').innerHTML = output;
      })
      .catch(err => console.log(err));
}

相关文章

网友评论

      本文标题:19.fetch(请求三种数据格式)

      本文链接:https://www.haomeiwen.com/subject/yzdusftx.html