ES6 fetch请求

先贴代码 看下 ajax 和fetch 之间的区别

{
//     原生ajax
    function ajax(){
       
           xmlHttp = new XMLHttpRequest(); 
           xmlHttp.open('get','https://api.github.com/users',true)
           xmlHttp.send()
            
                xmlHttp.onreadystatechange= callback;

                function callback(){
                    if(xmlHttp.readyState == 4){

                     //判断交互是否成功
                     if(xmlHttp.status == 200){
                       //获取服务器返回的数据
                       //获取纯文本数据
                       var responseText =xmlHttp.responseText;
                        console.log(JSON.parse(responseText))
                     }
                   }
                }
    }
    ajax()
}



//fetch
{
    fetch('https://api.github.com/users').then(response=> response.json())
        .then(data=>{
        console.log(data)
    })

}

还在整理  后续更

你可能感兴趣的:(javascript)