04_Fetchy.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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>Fetch</title>
  8. <style>
  9. table{
  10. border-collapse: collapse;
  11. width: 50%;
  12. }
  13. td,th{
  14. font-size: 20px;
  15. text-align: center;
  16. border: 1px solid #000;
  17. }
  18. caption{
  19. font-size: 30px;
  20. font-weight: bold;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h1>AJAX测试</h1>
  26. <hr>
  27. <button id="btn">点击加载数据</button>
  28. <br><br>
  29. <button id="btn02">点击加载数据2</button>
  30. <hr>
  31. <div id="root"></div>
  32. <script>
  33. const btn = document.getElementById("btn")
  34. const btn2 = document.getElementById("btn02")
  35. const root = document.getElementById("root")
  36. btn2.onclick = () =>{
  37. fetch("http://localhost:3000/students",{
  38. method:"post",
  39. //请求头,主要是告诉服务器,body的内容的类型是什么
  40. headers:{
  41. "Content-type":"application/json"
  42. //还有一种形式是application/x-www-form-urlencoded
  43. },
  44. //请求体
  45. //JSON.stringify:将对象转换成json来传送
  46. //通过body去发送数据时,必须通过请求头来指定数据的类型
  47. body:JSON.stringify({
  48. name:"白骨精",
  49. age:16,
  50. gender:"女",
  51. address:"白骨洞"
  52. })
  53. //body:"name=孙悟空&age=……"
  54. })
  55. }
  56. btn.onclick = () =>{
  57. /*
  58. fetch:
  59. -fetch是xhr的升级版,采用的是Promise API
  60. -作用和AJAX是一样的,但是使用起来更加友好
  61. -fetch是原生js就支持的一种ajax请求的方式
  62. */
  63. fetch("http://localhost:3000/students")
  64. //不管响应状态码是多少,只要服务器响应回来以后,then就会执行
  65. .then((res)=>{
  66. if(res.status === 200){
  67. //res.json() 可以用来读取json格式的数据,返回的是一个promise
  68. //console.log(res.json());
  69. //在then里面只要出现了promise,直接使用return返回
  70. return res.json()
  71. }else{
  72. throw new Error("加载失败!")
  73. //then里面无论出现什么异常,都可以抛错,最后在catch里面统一处理
  74. }
  75. })
  76. //上一步的返回值会返回到下一步,所以此处的then相当于是读取promise的数据
  77. .then(res=>{
  78. //console.log(res);
  79. //获取到数据后,将数据渲染到页面中
  80. if(res.status == "ok"){
  81. //创建一个table
  82. const table = document.createElement("table")
  83. root.appendChild(table)
  84. table.insertAdjacentHTML("beforeend","<caption>学生列表</caption>")
  85. table.insertAdjacentHTML("beforeend",`
  86. <thead>
  87. <tr>
  88. <th>学号</th>
  89. <th>姓名</th>
  90. <th>年龄</th>
  91. <th>性别</th>
  92. <th>地址</th>
  93. </tr>
  94. </thead>
  95. `)
  96. const tbody = document.createElement("tbody")
  97. table.appendChild(tbody)
  98. //遍历数据
  99. for(let stu of res.data){
  100. tbody.insertAdjacentHTML("beforeend",`
  101. <tr>
  102. <td>${stu.id}</td>
  103. <td>${stu.name}</td>
  104. <td>${stu.age}</td>
  105. <td>${stu.gender}</td>
  106. <td>${stu.address}</td>
  107. </tr>
  108. `)
  109. }
  110. }
  111. })
  112. .catch((err)=>{
  113. console.log("出错了!",err);
  114. })
  115. }
  116. </script>
  117. </body>
  118. </html>