手动实现一个ajax,不依赖第三方库
let xhr = new XMLHttpRequest();
xhr.open('method', '/url', true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText)
}
};
if (method === 'POST') {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
}
xhr.send();
存储
cookie , sessionStorage 和 localStorage的区别
cookie
本身用于客户端和服务器通信
具有本地存储的功能,于是被借用
使用document.cookie = 获取和修改
缺点:
- 存储量太小 ,只有4kb
- 所有http请求都带着 会影响资源获取效率
- api简单,需要封装才能使用
sessionStorage 和 localStorage
- html5专为存储设计最大容量5m
- api简单易用