封装一个 jQuery.ajax jQuery.ajax(url,method,body,success, fail) 满足这种 API。

window.jQuery.ajax = function (url,method,body,successFn,failFn) {
  let request = new XMLHttpRequest()
  request.open(method,url)
  request.onreadystatechange = () => {
    if (request.readyState === 4) {
        if (request.status >= 200 && request.status < 300) {
           successFn.call(undefined,request.responseText)
            }
        }else if (request.status >= 400) {
           failFn.call(undefined,request)
        }
    }
request.send(body)

你可能感兴趣的:(封装一个 jQuery.ajax jQuery.ajax(url,method,body,success, fail) 满足这种 API。)