SOURCE

/************  一、JS发送http请求的5个步骤 *********** */
/*
const url = 'https://elm.cangdu.org/v4/restaurants?geohash=40.003643,116.323044&keyword=%E8%9B%8B%E7%B3%95'

// 1.创建XHR对象
const xhr = new XMLHttpRequest()
// 2.注册事件(拦截请求和响应)
xhr.onreadystatechange = function(){
    // readyState含义:0表示刚创建XHR未初始化 1表示open()后, 2表示send()后, 3表示客户端正在接受数据
    // 4表示接受数据完毕
    if(xhr.readyState === 1){
        console.log('拦截请求')
    }else if(xhr.readyState === 4){
        if(xhr.status === 200){
            dealResponse(JSON.parse(xhr.responseText))
        }else{
            console.log('error: '+xhr.status)
        }
    }

}
// 3.设置请求方法和url
xhr.open('get', url)

// 4.发送请求
xhr.send()

// 5.处理响应
function dealResponse(data){
    console.log(data)
}

*/



/*********** 二、实现ajax *********** */
/*
const ajax = {
    get: function(url, succeed, fail){
        const xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function(){
            if(xhr.readyState===4){
                if(xhr.status === 200 || xhr.status === 304){
                    succeed(xhr.responseText)
                }else{
                    fail(xhr.responseText)
                }
            }
        }
        xhr.open('get', url)
        xhr.send()
    },

    post: function(url,data, succeed, fail){
        const xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function(){
            if(xhr.readyState===4){
                if(xhr.status === 200 || xhr.status === 304){
                    succeed(xhr.responseText)
                }else{
                    fail(xhr.responseText)
                }
            }
        }
        xhr.open('post', url)
        xhr.send(data)
    }
}
*/


console 命令行工具 X clear

                    
>
console