Vue - axios

Vue

import axios from "axios";

Axios 

发送GET请求

 

      axios({
          // 请求方式
          method: "GET",
          // 请求地址
          url: "http://www.liulongbin.top:3006/api/getbooks",
          // URL中的查询参数
          params: {
            id: 2,
          },
        }).then(function(result) {
            console.log(result);
        })
      });

 

发送post请求

<!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>
  </head>
  <body>
    <button id="btnPost">发送POST请求</button>
    <!-- 引入axios -->
    <script src="/lib/axios.js"></script>
    <script>


      document.querySelector('#btnPost').addEventListener('click', function() {
        axios({
            method: 'POST',
            url: 'http://www.liulongbin.top:3006/api/post',
            // 请求体参数
            data: {
                name: 'zs',
                age: 12,
            }
        })
      })
    </script>
  </body>
</html>

 

axios.get

methods: {
    inserts() {
      axios.get("http://localhost:8086/select/").then((result) => {
        console.log(result.data);
        this.count = result.data;
      });
    },
  },
};

 

posted @ 2022-10-27 23:32  しゅおく  阅读(30)  评论(0)    收藏  举报