• AJAX (Asynchronous JavaScript And XML) 就是用 JS 执行异步网络请求
  • AJAX 是浏览器上的功能,浏览器可以发请求与收响应
  • 浏览器在 window 上加了一个构造函数 XMLHttpRequest
  • JS通过这个函数构造出的对象可以实现发请求收响应的功能
  1. > window.XMLHttpRequest // ƒ XMLHttpRequest() { [native code] }
  2. > typeof window.XMLHttpRequest // "function"

使用方法

  1. 创建 HttpRequest对象(全称 XMLHttpRequest)
  2. 调用对象的 open 方法
  3. 监听对象的 onload 和 onerror事件(专业前端改用 onreadystatechange 事件, 因为onerror事件不会触发)
  4. 在事件处理函数里操作CSS内容
    • 加载 CSS : <style>, innerHTML
    • 加载 JS : <script>, innerHTML
    • 加载 HTML : <div>, innerHTML, DOM API
    • 加载 XML : responseXML , DOM API
    • 加载JSON : JSON.parse
  5. 调用对象的 send 方法(发现请求)
  1. getCSS.onclick = () => {
  2. const request = new XMLHttpRequest(); // readyState === 0
  3. request.open('GET', 'style.css'); // readyState === 1
  4. request.onreadystatechange = () => {
  5. if (request.readyState === 4) {
  6. // 成功 2xx,失败 4xx 5xx
  7. if (request.status >= 200 && request.status < 300) {
  8. console.log(1, request.response);
  9. const style = document.createElement('style');
  10. style.innerHTML = request.response;
  11. document.head.appendChild(style);
  12. }else{
  13. alert('加载失败');
  14. }
  15. }
  16. }
  17. // request.onload = () => {
  18. // const style = document.createElement('style');
  19. // style.innerHTML = request.response;
  20. // document.head.appendChild(style);
  21. // }
  22. // request.onerror = () => {
  23. // alert('加载失败');
  24. // }
  25. request.send(); // readyState === 2
  26. }
  27. getJS.onclick = () => {
  28. const request = new XMLHttpRequest();
  29. request.open('GET', '2.js');
  30. request.onreadystatechange = () => {
  31. if (request.readyState === 4) {
  32. if (request.status >= 200 && request.status < 300) {
  33. console.log(1, request.response);
  34. const script = document.createElement('script');
  35. script.innerHTML = request.response;
  36. document.body.appendChild(script);
  37. }else{
  38. alert('加载失败');
  39. }
  40. }
  41. }
  42. request.send();
  43. }
  44. getHTML.onclick = () => {
  45. const request = new XMLHttpRequest();
  46. request.open('GET', '3.html');
  47. request.onreadystatechange = () => {
  48. if (request.readyState === 4) {
  49. if (request.status >= 200 && request.status < 300) {
  50. // console.log(1, request.responseXML);
  51. const div = document.createElement('div');
  52. div.innerHTML = request.response;
  53. document.body.appendChild(div);
  54. }else{
  55. alert('加载失败');
  56. }
  57. }
  58. }
  59. request.send();
  60. }
  61. getXML.onclick = () => {
  62. const request = new XMLHttpRequest();
  63. request.open('GET', '4.xml');
  64. request.onreadystatechange = () => {
  65. if (request.readyState === 4) {
  66. if (request.status >= 200 && request.status < 300) {
  67. const dom = request.responseXML;
  68. // console.log(1, request.responseXML);
  69. const text = dom.getElementsByTagName('warning')[0].textContent;
  70. console.log(text.trim()); // text.trim() 去掉 text 前后的空格
  71. }else{
  72. alert('加载失败');
  73. }
  74. }
  75. }
  76. request.send();
  77. }
  78. getJSON.onclick = () => {
  79. const request = new XMLHttpRequest();
  80. request.open('GET', '5.json');
  81. request.onreadystatechange = () => {
  82. if (request.readyState === 4) {
  83. if (request.status >= 200 && request.status < 300) {
  84. // console.log(request.response);
  85. const object = JSON.parse(request.response);
  86. // console.log(object);
  87. ZhuzhuName.textContent = object.name;
  88. }else{
  89. alert('加载失败');
  90. }
  91. }
  92. }
  93. request.send();
  94. }

readState

XMLHttpRequest.readyState 属性返回一个 XMLHttpRequest 代理当前所处的状态

一个请求的生命周期:

  1. readState
  2. const req = new XMLHttpRequest() ------ 0
  3. req.open() --------------- 1
  4. req.send() --------------- 2
  5. 页面出现第一个信息 --------------- 3
  6. 页面信息下载完 --------------- 4

AJAX面试代码

  1. var request = new XMLHttpRequest();
  2. request.open('GET', '/xx');
  3. request.onreadystatechange = function() {
  4. if(request.readyState === 4){
  5. console.log('请求完成');
  6. if(request.response.status >= 200 && request.response.status < 300) {
  7. console.log('请求成功')
  8. }else{
  9. console.log(request.status)
  10. }
  11. }
  12. }
  13. request.send()

练习