优点:不用刷新页面,可以请求任何内容
缺点:不能跨域,难记
AJAX
const ajax =(method,path,body,successFn,failFn) =>{
const xhr=new XMLHttpRequest()
xhr.open(method,path)
xhr.onreadystatechange =() =>{ if (xhr.readyStae===4) {
if (xhr.status>=200&&xhr.status<300) {
console.log('请求成功,得到内容为:'+xhr.responseText)
successFn(xhr)
}else if (xhr.status>=400){
console.log('请求失败,状态码为:'+xhr.status)
failFn(xhr) }
}
}
xhr.send(body) }
successFn: (xhr) => {
console.log('成功了')
}
failFn: (xhr) => {
console.log('失败了')
}