function getData(url){
var pro = new Promise(function(resolve,reject){
var xhr = null;
try{
xhr = new XMLHttpRequest()
}
catch(e){
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("get",url);
xhr.onreadystatechange=function (){
if(xhr.readyState===4){
if(xhr.status===200){
resolve(xhr.response);
}else{
reject(new Error(xhr.status));
}
}
}
xhr.send()
})
return pro;
}
getData("xxxx.url").then(function(data){
console.log(data);
},function(error){
console.log(error);
})
readyState
0:已建立对象但没有send
1:已经send对象正在载入
2:载入完成
3:解析接收到数据
4:解析完成,可以response了
状态 status
200:ok
302:文件暂时性转移
304:资源在上次请求后没有修改
500:服务器内部问题