1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| function ajax(url) { return new Promise(function (resolve,reject) { var xml = new XMLHttpRequest() xml.open('GET', url, true); xml.send(null); xml.onreadystatechange = function () { if (xml.readyState === 4) { if (xml.status === 200) { // 请求成功 resolve(xml.responseText) } else { reject(new Error('请求失败状态码:'+ xml.status)) } } } }) }
ajax('http://www.xxxx.com').then(res-0 =>{ console.log(res-0) return ajax('http://www.ccc.com') }).then(res-1 =>{ console.log(res-1) }).catch(err =>{ console.log(err) })
|