function add(url) {
var xhr = new XMLHttpRequest();
return new Promise((resolve, reject) => {
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
resolve(JSON.parse(xhr.responseText));
}
}
xhr.open('GET', url, false); // false 表示同步处理,true 表示异步处理
xhr.send(null);
})
}
add('https://www.zhengzhicheng.cn/api/public/v1/home/swiperdata').then(res => {
console.log(res);
})
promise 封装的 原生ajax
最新推荐文章于 2021-09-26 19:56:27 发布