ajax 封装

export request = function(url, method, success, fail) {
  let ajax = null;
  if (window.XMLHttpRequest) {
     ajax = new XMLHttpRequest();
  } else {
    ajax = new ActiveXObject('Microsoft.XMLHTTP');
  }
  ajax.onreadystatechange = function() {
    if (ajax.readyState === 4 && ajax.status === 200) {
      success && success(ajax.responseText);
    } else {
      fail && fail();
    }
  };
  ajax.open(method, url, true);
  ajax.send();
}

你可能感兴趣的:(ajax 封装)