原生ajax

  // 原生ajax  (最简单版本GET请求,未处理IE兼容)
    const xhr = XMLHttpRequest();
    xhr.open("GET", "/api", true);
    xhr.onreadystatechange = function(){
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          alert(xhr.responseText);
        }
      }
    }
    xhr.send(null);  // 如果是POST请求,send的参数就是提交的数据

    // 关于readyState 状态码
    // 0 - (未初始化) 还没有调用send()方法
    // 1 - (载入) 已调用send()方法,正在发送请求
    // 2 - (载入完成) send()方法执行完成,已经收到全部响应的内容
    // 3 - (交互) 正在解析响应内容
    // 4 - (完成) 响应内容解析完成,可以在客户端调用了
原文地址:https://www.cnblogs.com/liangzhixiaolaohu/p/13172743.html