Blogger Information
Blog 94
fans 0
comment 0
visits 92763
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS】异步:传统XHR(AJAX)-过时了
可乐随笔
Original
1571 people have browsed it

异步:传统XHR(AJAX)-过时了

1.传统的XHR: GET

总结:

  1. 创建对象: new XMLHttpRequest()
  2. 响应类型: xhr.responseType = 'json'
  3. 配置参数: xhr.open("GET / POST", url, true)
  4. 请求回调: xhr.onload = () => console.log(xhr.response)
  5. POST:xhr.setRequestHeater('content-type','application/json')
  6. 失败回调: xhr.onerror = () => console.log('Error')
  7. 发起请求: xhr.send(null / jsonString)

XHR 示范HTML:

  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>传统的XHR: GET</title>
  8. </head>
  9. <body>
  10. <button onclick="getUser(this)">XHR请求-GET</button>
  11. <script>
  12. function getUser(btn) {
  13. // 1. 创建对象
  14. const xhr = new XMLHttpRequest();
  15. // 2. 响应类型
  16. xhr.responseType = 'json';
  17. // 3. 配置参数
  18. // xhr.open(请求类型,请求URL,请求方式)
  19. xhr.open('GET', 'http://ajaxtest.cn/data.php?id=2');
  20. // 4. 成功回调
  21. xhr.onload = function () {
  22. // xhr.response: 响应数据
  23. console.log(xhr.response);
  24. // DOM: 将响应数据渲染到页面中
  25. };
  26. // 5. 失败回调
  27. xhr.onerror = function () {
  28. console.log('Error');
  29. };
  30. // 6. 发送请求
  31. xhr.send(null);
  32. }
  33. </script>
  34. </body>
  35. </html>

2.传统的XHR: POST

  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>传统的XHR: POST</title>
  8. </head>
  9. <body>
  10. <form action="" onsubmit="return false">
  11. <input type="text" />
  12. <input type="text" />
  13. <input type="text" />
  14. <button onclick="getUser(this)">XHR-POST请求</button>
  15. </form>
  16. <!-- <button onclick="getUser(this)">XHR-POST请求</button> -->
  17. <script>
  18. function getUser(btn) {
  19. // 1. 创建对象
  20. const xhr = new XMLHttpRequest();
  21. // 2. 响应类型
  22. xhr.responseType = 'json';
  23. // 3. 配置参数
  24. // xhr.open(请求类型,请求URL,请求方式)
  25. xhr.open('POST', 'http://ajaxtest.cn/data.php?id=2');
  26. // 4. 设置请求头(POST)
  27. xhr.setRequestHeater('content-type', 'application/json');
  28. // 4. 成功回调
  29. xhr.onload = function () {
  30. // xhr.response: 响应数据
  31. console.log(xhr.response);
  32. // DOM: 将响应数据渲染到页面中
  33. };
  34. // 5. 失败回调
  35. xhr.onerror = function () {
  36. console.log('Error');
  37. };
  38. // 6. 发送请求
  39. xhr.send(JSON.stringify({ name: 'admin', email: 'admin@php.cn' }));
  40. }
  41. </script>
  42. </body>
  43. </html>
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