vuejs之使用axios发送http请求

时间:2022-07-24
本文章向大家介绍vuejs之使用axios发送http请求,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

看一个例子:

<html>

<head>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
  <input type="button" value="get请求" class="get">
  <input type="button" value="post请求" class="post">
  <script>
    document.querySelector(".get").onclick = function () {
      axios.get('https://autumnfish.cn/api/joke/list?num=3')
        .then(function (response) {
          console.log(response);
        })
    }
    document.querySelector(".post").onclick = function () {
      axios.post('https://autumnfish.cn/api/user/reg', { username: "西西嘛呦" })
        .then(function (response) {
          console.log(response);
        })
    }

  </script>
</body>

</html>

效果:

点击get请求:

点击post请求:

说明:

引入:<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

格式:axios.请求方式(请求网址).then(function(response){})。

如果是get请求,在地址后面用?带上参数,如果是post请求,在post里再增加一个参数字典。