const ajax = ({ method, path, body, successFn, failFn }) => {
const xhr = new XMLHttpRequest()
xhr.open(method, path) // 1
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
successFn(xhr) // 2
} else if (xhr.status >= 400) {
failFn(xhr) // 3
}
}
}
xhr.send(body)
}
Ajax优点:
1. 可以请求任何内容(但是不能跨域)
2. 不用刷新页面
Ajax缺点:
1. 代码难记
2. 不能跨域(小白才会认为这是缺点)