看一个例子:

<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>

效果:

vuejs之使用axios发送http请求

点击get请求:vuejs之使用axios发送http请求

点击post请求:

vuejs之使用axios发送http请求

说明:

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

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

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

相关文章:

  • 2022-01-13
  • 2021-07-25
  • 2022-02-09
  • 2022-01-01
  • 2021-09-14
  • 2021-08-24
猜你喜欢
  • 2022-02-08
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-24
  • 2021-05-28
相关资源
相似解决方案