// Promise Ajax方法 function ajax(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200) { resolve(JSON.parse(xhr.responseText)) } else if (xhr.status === 404) { reject(new Error('404 not found')) } } } xh
1.手写简易Ajax//Ajax - GET请求 const xhr = new XMLHttpRequest(); // true 为异步,false为同步 xhr.open('GET', 'URL', false) xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200) { console.log(xhr.response); } } } xhr.send(null) //Ajax - POST请求 const post = new XMLHttpRequest(); // true 为异步,false为同步 post.open('POST', 'URL', true); post.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200) {
Gonwe
一念智即般若生。
CC BY-SA 4.0