一、下载安装

官网:https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API

二、使用

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>TestAjax</title>
  6. </head>
  7. <body>
  8. <button>发送请求</button>
  9. <script>
  10. const btn = document.querySelector('button');
  11. btn.onclick = function (){
  12. fetch('http://localhost:8000/fetch',{
  13. method : 'POST',
  14. headers:{
  15. name:'mx'
  16. },
  17. body:'username=admin&password=admin',
  18. }).then(response => {
  19. // return response.text();
  20. return response.json();
  21. }).then(response=>{
  22. console.log(response);
  23. });
  24. }
  25. </script>
  26. </body>
  27. </html>