<script>
function sendAjax(url) {
return new Promise((resolve, reject) => {
// 创建对象
const xhr = new XMLHttpRequest();
//修改响应体的格式
xhr.responseType = "json";
// 初始化
xhr.open("GET", url);
// 发送请求
xhr.send();
// 绑定事件处理响应结果
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response);
} else {
reject(xhr.status);
}
}
};
});
}
sendAjax("http://gmall-h5-api.atguigu.cn/api/product/getBaseCategoryList")
.then((value) => {
console.log(value);
})
.catch((reason) => {
console.log(reason);
});
</script>
[Promise]封装Ajax请求
最新推荐文章于 2022-11-29 14:15:51 发布