ES6封装http请求

    http (data) {
        return new Promise((resolve, reject) => {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
                        resolve(xhr.responseText);
                    } else {
                        reject(xhr.status);
                    }
                } else {
                    // HTTP请求还在继续...
                }
            };
            xhr.open(data.methods, data.url, true);
            xhr.setRequestHeader('content-type', 'application/json');
            delete data.methods;
            delete data.url;
            xhr.send(JSON.stringify(data));
        });
    }

  

猜你喜欢

转载自www.cnblogs.com/mlh1421/p/10071672.html