天天看点

04——axios创建实例对象发送请求

<!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" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
    />
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.js"></script>
  </head>
  <body>
    <div>
      <h2 class="page-header">基本使用</h2>
      <button class="btn btn-primary">发送GET请求</button>
      <button class="btn btn-warning">发送POST请求</button>
    </div>
    <script>
     const btns = document.querySelectorAll("button");
     const duixiang=axios.create({
       baseURL:'https://api.apiopen.top',
       timeout:2000
     })
     //方法一
    //  duixiang({
    //    url:'/getJoke'
    //  }).then(response=>{
    //    console.log(response);
    //  })
  //方法二 借助封装好的方法
  duixiang.get('/getJoke').then(response=>{
    console.log(response);
  })
    </script>
  </body>
</html>

           

继续阅读