封装promise

const getJSON = function(url,type,data) {
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(type, url);
client.onreadystatechange = handler;
client.responseType = "json";
if(type=='get'){
client.send();
}else {
client.setRequestHeader("Content-Type","application/json");//data只能是JSON字符串
client.send(JSON.stringify(data)) //post请求传入string
}
 
});
return promise;
};
 
 
 
调用案例
$(function() {
  $("button").click(function() {  
     getJSON('http://localhost:3000/info','get').then( res => {
      //success
     console.log('ok');
     }).catch( res=> {
          console.error('出错了', error);
    });
  });
//JQUERY 1.5.0返回的是xhr对象 高于1.5.0返回的deferred对象
})
posted @ 2018-07-03 11:27  躁动的倾听者  阅读(158)  评论(0编辑  收藏  举报