使用promise写一个简单的ajax案例:
function ajax (url) {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.responseType = 'json'
xhr.onload = function () {
if (this.status + '' === '200') {
resolve(this.response)
console.log(this.response)
} else {
reject(new Error(this.responseText))
console.log(this.responseText)
}
}
xhr.send()
})
}
ajax('./ajaxTest.json')