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里再增加一個參數字典。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM