013 Promise对象_Ajax实操

Promise封装Ajax,让网络请求的异步操作变得更简单

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //XHR对象
        const getJSON=function(url){
            const promise=new Promise(function(resolve,reject){
                //异步操作:网络请求代码
                const handler=function(){
                    if(this.readyState!==4){
                        // 0 1 2 3 4
                        return;
                    }
                    if(this.status===200){
                        resolve(this.response)
                    }else{
                        reject(new Error(this.statusText))
                    }
                }

                const client=new XMLHttpRequest();
                client.open("GET",url);
                client.onreadystatechange=handler;
                client.responseType="json";
                client.setRequestHeader("Accept","application/json");
                client.send();
            })

            return promise;
        }

        getJSON("http://........")//网络请求的地址,接口
        .then(function(data){
            console.log(data);
        },function(error){
            console.log(error);
        })
    </script>
</body>
</html>

 

 

posted @ 2025-02-11 13:46  张筱菓  阅读(9)  评论(0)    收藏  举报