- ajax在不刷新页面的情况下可以实现局部刷新
- 使用promise封装ajax
function ajaxPromis() {
let promise = new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
//true异步false同步
xhr.open('get', 'url', true)
xhr.onreadyStatechange = () => {
if(xhr.readyState === 4) {
//304从浏览器缓存中获取数据
if(xhr.state >= 200 & xhr.state < 300 || xhr.state === 304){
resolve(xhr.responseURL)
}
else {
reject(new Error(xhr.statusText)
}
}
}
xhr.send(null)
})
return promise
}