es6中promise实现ajax的例子
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:服务器内部问题
Promise.all的使用
Promise.all([ getApplicationManage(params), msgListConfig(), emailListConfig(), wechatListConfig(), getModuleManage(params) ]).then(function(values) { _this.applicationData = values[0].model; _this.msgConfigOptions = values[1].model; _this.emailConfigOptions = values[2].model; _this.wechatConfigOptions = values[3].model; let resData = values[4].model; }).catch(error => { console.log('error'); }).finally(() => { this.loading = false; console.log('finally'); });