Promise封装getJSON:
const getJSON = function(url) {
const promise = new Promise(function(resolve, reject){
const handler = function() {
if (this.readyState !== 4) {
return;
}
if (this.status === 200) {
resolve(this.response);
} else {
reject(new Error(this.statusText));
}
};
const client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = handler;
client.responseType = "json";
client.setRequestHeader("Accept", "application/json");
client.send();
});
return promise;
};
getJSON("/posts.json").then(function(json) {
console.log('Contents: ' + json);
}, function(error) {
console.error('出错了', error);
});
Promise封装ajax的get、post请求:
const ajaxhttp = function(url,type) {
const promise = new Promise(function(resolve, reject){
if(type === "get"){
$.get(url, (data) => {
if(data.status === "200") {
resolve(data);
} else {
reject(data.err);
}
});
}
if(type === "post"){
$.post(url, (data) => {
if(data.status === "200") {
resolve(data);
} else {
reject(data.err);
}
});
}
});
return promise;
};
ajaxhttp("/code/getauthcode.do","get").then(function(data) {
console.log(data);
}, function(error) {
console.error(error);
});