问题描述:想通过 promise 对象来实现 ajax数据请求
//getDate.json
{
"name":"zzz",
"age":22,
"say":"hello zzz"
}
//demo.js
let getJson = url =>{
let promise = new Promise((resolve,reject) =>{
let xhr = new XMLHttpRequest();
xhr.open("GET",url,true);
xhr.onreadystatechange = () =>{
if(this.readyState !== 4){
return;
}
if(this.status == 200){
resolve(this.response);
}else{
reject(new Error(this.statusText))
}
}
xhr.responseType = 'json';
xhr.setRequestHeader('Accept','application/json');
xhr.send(null);
});
return promise;
};
getJson("getDate.json").then((json) =>{console.log(json);},(err)=>{console.log(err)});
抛出错误:(intermediate value) is not a constructor
宝慕林4294392
沧海一幻觉
随时随地看视频慕课网APP
相关分类