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