Blogger Information
Blog 28
fans 0
comment 0
visits 13122
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示 fetch / async / await 的使用场景
手机用户1594223549
Original
528 people have browsed it

代码部分

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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 / async / await 的使用场景</title>
  8. </head>
  9. <body>
  10. <script>
  11. // fetch的使用
  12. fetch("https://jsonplaceholder.typicode.com/todos/7")
  13. // 获取一些json数据,在项目中进行测试
  14. .then(function (response) {
  15. return response.json()
  16. })
  17. .then(function (json) {
  18. console.log(json)
  19. })
  20. </script>
  21. <script>
  22. // async / await的使用
  23. // function 前添加 async , 转为异步函数
  24. async function getUser() {
  25. let url = 'https://jsonplaceholder.typicode.com/todos/7';
  26. // 1. 等待结果再进行下一步操作,返回响应对象
  27. const response = await fetch(url);
  28. // 2. 将响应结果,转为json, json()
  29. const result = await response.json();
  30. console.log(result);
  31. }
  32. </script>
  33. </body>
  34. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post