时间: 2020-09-10|tag:40次围观|0 条评论

function ajaxMise(url, method, data, async, timeout) {    var xhr = new XMLHttpRequest()    return new Promise(function (resolve, reject) {        xhr.open(method, url, async);        xhr.timeout = options.timeout;        xhr.onloadend = function () {            if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304)                resolve(xhr);            else                reject({                    errorType: 'status_error',                    xhr: xhr                })        }        xhr.send(data);        //错误处理        xhr.onabort = function () {            reject(new Error({                errorType: 'abort_error',                xhr: xhr            }));        }        xhr.ontimeout = function () {            reject({                errorType: 'timeout_error',                xhr: xhr            });        }        xhr.onerror = function () {            reject({                errorType: 'onerror',                xhr: xhr            })        }    })}

文章转载于:https://www.jianshu.com/p/35107f166a38

原著是一个有趣的人,若有侵权,请通知删除

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《用Promise封装一下原生ajax
   

还没有人抢沙发呢~