0%

806_axios_其他方法使用

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      crossorigin="anonymous"
      href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
      rel="stylesheet"
    />
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
  </head>
  <body>
    <div class="container">
      <h2 class="page-header">基本使用</h2>
      <button class="btn btn-primary">发送GET请求</button>
      <button class="btn btn-warning">发送POST请求</button>
      <button class="btn btn-success">发送PUT请求</button>
      <button class="btn btn-danger">发送DELETE请求</button>
    </div>
    <script>
      //获取按钮
      const btns = document.querySelectorAll('button');

      //发送GET请求
      btns[0].onclick = function () {
        //axios()
        axios
          .request({
            method: 'GET',
            url: 'http://localhost:3000/comments',
          })
          .then(v => {
            console.log(v);
          });
      };

      //发送POST请求
      btns[1].onclick = function () {
        //axios()
        axios
          .post('http://localhost:3000/comments', {
            body: '喜大普奔',
            postId: 2,
          })
          .then(v => {
            console.log(v);
          });
      };
    </script>
  </body>
</html>