SOURCE

console 命令行工具 X clear

                    
>
console
<script>
const xhr = new XMLHttpRequest()
xhr.open('GET', '/a', true)
xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 ) {
        if(xhr.status === 200) {
        } else {
        }
    }
}  
// post 需要传json字符串  xhr.send(JSON.stringfy(data))
xhr.send(null)
/**
 * readyState: 0-未初始化 1-载入,一调用send()正在发生 2-载入完成 send执行完成已接收的全部数据 3-交互 正在解析相应 4-解析完成 可以在客户端调用 
 * 
 * */
/**
 * 同源策略  ajax请求,浏览器要求 当前网页和server必须同源(安全)
 * 同源:协议、域名、端口 三者必须一致
 * 
 * 跨域常见实现方式: JSONP, cors 服务端设置 http header
 * JSONP 通过script 地址,window里定义一个callback, 在jsonp中去执行把数据作为参赛传回来
*/
function ajax(url) {
    const p =  new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', '/a', true)
        xhr.onreadystatechange = function() {
            if(xhr.readyState === 4) {
                if (xhr.status === 200) {
                    resolve(123)
                } else {
                    reject('33333333')
                }
            }
        }
        xhr.send(null)
    })
    return p
}
ajax('/1234').catch((e) => {console.log(e)})
</script>