axios调用接口返回土味情话

205 阅读1分钟
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://unpkg.com/axios@0.24.0/dist/axios.min.js"></script>

    <title>Document</title>
    <style>
      p {
        font-weight: bold;
        font-size: 22px;
        color: orangered;
      }
    </style>
  </head>
  <body>
    <h1>每天一句土味情话</h1>
    <p></p>
  </body>
  <script>
    // 找到p标签,下一步要替换里面的innerHTML
    const p = document.querySelector("p");
    // 定义一个url
    const url = "https://api.vvhan.com/api/love?type=json";
    // 使用axios请求接口
    axios
      .get(url)
      .then(function (response) {
        // console.log(response);
        // data和ishan的由来可以打印
        // console.log(response)就看出来了;
        const text = response.data.ishan;
        // 替换到页面
        p.innerHTML = text;
      })
      .catch(function (err) {
        console.log(err);
      });
  </script>
</html>