function getJSON(url) {
return new Promise(function(resolve, reject) {
var XHR = new XMLHttpRequest();
XHR.open('GET', url, true);
XHR.send();
XHR.onreadystatechange = function() {
if (XHR.readyState == 4) {
if (XHR.status == 200) {
try {
var response = JSON.parse(XHR.responseText);
resolve(response);
}
catch (e) {
reject(e);
}
}
else {
reject(new Error(XHR.statusText));
}
}
}
})}
getJSON(url).then(resp => console.log(resp));
用promise封装一个简单的ajax啊
本文转载:CSDN博客