手動實現一個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簡單易用