阅读背景:

Promise 简单封装Ajax

来源:互联网 
!-- flowchart 箭头图标 勿删 --

get请求

function getJSON (url) {
    return new Promise( (resolve, reject) => {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.onreadystatechange = function () {
        	// xhr 为 this
        	var resJson;
            if (this.readyState === 4) {
                if (this.status === 200) {
                    resolve(this.responseText, this);
                } else {
                    resJson = { code: this.status, response: this.response };
                    reject(resJson, this);
                }
            }
        }
        xhr.send();
    });
}
f



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: