fetch 发送 AJAX请求

悠悠 2023-10-06 17:20 192阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>fetch 发送 AJAX请求</title>
  7. </head>
  8. <body>
  9. <button>AJAX请求</button>
  10. <script>
  11. //文档地址
  12. //https://developer.mozilla.org/zh-CN/docs/Web/API/WindowOrWorkerGlobalScope/fetch
  13. const btn = document.querySelector('button');
  14. btn.onclick = function(){
  15. fetch('http://127.0.0.1:8000/fetch-server?vip=10', {
  16. //请求方法
  17. method: 'POST',
  18. //请求头
  19. headers: {
  20. name:'atguigu'
  21. },
  22. //请求体
  23. body: 'username=admin&password=admin'
  24. }).then(response => {
  25. //返回promise对象
  26. // return response.text();
  27. return response.json();
  28. }).then(response=>{
  29. console.log(response);
  30. });
  31. }
  32. </script>
  33. </body>
  34. </html>

发表评论

表情:
评论列表 (有 0 条评论,192人围观)

还没有评论,来说两句吧...

相关阅读