发送ajax时open那里第三个参数如果是false,则会给一个警告,没有报错网络里也正常发出请求了但是不会执行onreadystatechange里的代码;如下代码将ajax用promise包裹控制台里同样发现正常请求了但是console中会直接报错打印请求失败
let readUrlPromise=url=>{
return new Promise((resolve,reject)=>{
let xhr=new XMLHttpRequest();
xhr.open("GET",url);
xhr.send(null);
xhr.onreadystatechange=function(){
if(xhr.readyState==4 && xhr.status==200){
// console.log(JSON.parse(xhr.responseText));
resolve(JSON.parse(xhr.responseText));
}else{
reject('请求失败');
}
}
})
}
readUrlPromise("https://devapi.qweather.com/v7/weather/24h?location=经纬度&key=和风天气key").then(data=>{
console.log(data);
})