方法一

  1. const ajax = (method, url, data, success, fail) => {
  2. var request = new XMLHttpRequest()
  3. request.open(method, url)
  4. request.onreadystatechange = function() {
  5. if(request.readyState === 4) {
  6. if(request.status >= 200 && request.status < 300 || request.status === 304) {
  7. success(request)
  8. } else {
  9. fail(request)
  10. }
  11. }
  12. }
  13. request.send(data)
  14. }

方法二

  1. const ajax = (method, url, data, success, fail) => {
  2. var request = new XMLHttpRequest()
  3. request.open(method, url)
  4. request.onload = function(){
  5. if(request.status >= 200 && request.status < 300 || request.status === 304) {
  6. success(request)
  7. } else {
  8. fail(request)
  9. }
  10. }
  11. request.send(data)
  12. }