10. 用promise封装ajax请求

2023-08-15 13:33:36发布
20
function sendAajax(url) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.responseType = 'json';
        xhr.open("GET", url, true);
        xhr.send();
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    resolve(xhr.response)
                } else {
                    reject(xhr.status)
                }
            }
        }
    });
}

sendAajax("http://111.230.246.226:8088/getList?nowPage=1")
.then((value) => {
    console.log(value)
}).catch((status) => {
    console.log(status)
});