Ajax:(async javascript and xml)
四步创建
function getRequest() {
return new Promise((resolve, reject) => {
// 1.
const xhr = new XMLHttpRequest();
/**2.xhr.open([http method], [url], [async], [userName], [userPass])
http methods 有常用的请求方式有:post,get,delete,put,head,options,trace,connect。
url:是想服务器请求的 api。
async:代表异步,默认是 true 异步,false 是同步。
userName、userPass:代表用户名和密码
7 */
xhr.open('GET', url, false);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
/**3.Ajax 状态一共有 5 种 xhr.readyState,分别是 0, 1, 2, 3, 4
状态 0:unsent,刚创建的 XMLHttpRequest 实例,还没有发送。
状态 1:(载入)已调用 send() 方法,正在发送请求。
状态 2:(载入完成)send() 方法执行完成,已经接收到全部响应内容
状态 3:loading(交互)正在解析响应内容
状态 4:done 表示响应的主体内容解析完成,可以在客户端调用了 */
if (xhr.readyState !== 4) return;
if (xhr.