# 手写 ajax 原理

ajax 原理

function ajax(url) {
  return new Promise(function (resolve, reject) {
    // foo()
    // throw new Error()
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.responseType = "json";
    xhr.onload = function () {
      if (this.status === 200) {
        resolve(this.response);
      } else {
        reject(new Error(this.statusText));
      }
    };
    xhr.send();
  });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Last Updated: 3/6/2022, 8:19:57 PM
강남역 4번 출구
Plastic / Fallin` Dild