1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
7. <title>Document</title>
8. </head>
9. <body>
10. <!-- 导入axios库 -->
11. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
12. <input type="button" value="get请求" id="get">
13. <input type="button" value="post请求" id="post">
14. <script>
15. /*
16. 接口1:随机笑话
17. 请求地址:https://autumnfish.cn/api/joke/list
18. 请求方法:get
19. 请求参数:num(笑话条数,数字)
20. 响应内容:随机笑话
21. */
22. document.querySelector('#get').onclick=function(){
23. axios.get('https://autumnfish.cn/api/joke/list?num=5')
24. // 错误的url 走err
25. // axios.get('https://autumnfish.cn/api/joke/list11?num=5')
26. .then(
27. function(response){
28. console(response);
29. },
30. function (err) {
31. console.log(err);
32. }
33. )
34. }
35. /*
36. 接口2:用户注册
37. 请求地址:https://autumnfish.cn/api/user/reg
38. 请求方法:post
39. 请求参数:username(用户名,字符串)
40. 响应内容:注册成功或失败
41. */
42. document.querySelector('#post').onclick=function(){
43. axios.post('https://autumnfish.cn/api/user/reg',{username:'测试小白'})
44. .then(
45. function(response){
46. alert(response.data);
47. },
48. function(err){
49. console.log(err);
50. }
51. )
52. }
53. </script>
54.
55. </body>
56. </html>