async和await结合封装一个ajax请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //发送ajax请求,返回的结果是一个promise对象
        function sendAjax(url){

            return new Promise((reslove,reject) => {
                //1.创建对象
                const x = new XMLHttpRequest();
                //2.初始化
                x.open('GET',url,true);
                //3.发送
                x.send();
                //4.事件绑定
                x.onreadystatechange = function(){
                    if(x.readyState == 4){
                        if(x.status>=200 && x.status<=300){ 
                            // 成功了
                            reslove(x.response);
                        }else{
                            //返回失败
                            reject(x.status);
                        }
                    }
                }
            })


        }

        // Promise then方法的测试
        sendAjax('http://api.k780.com/?app=weather.today&weaId=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json').then((value) => {
            console.log(value);
        },(reason) => {
            
        })

        async function main(){
            //发送请求
            let result = await sendAjax('http://api.k780.com/?app=weather.today&weaId=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json');
            console.log(result);
        }
        main(); 
    </script>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //发送ajax请求,返回的结果是一个promise对象
        function sendAjax(url){

            return new Promise((reslove,reject) => {
                //1.创建对象
                const x = new XMLHttpRequest();
                //2.初始化
                x.open('GET',url,true);
                //3.发送
                x.send();
                //4.事件绑定
                x.onreadystatechange = function(){
                    if(x.readyState == 4){
                        if(x.status>=200 && x.status<=300){ 
                            // 成功了
                            reslove(x.response);
                        }else{
                            //返回失败
                            reject(x.status);
                        }
                    }
                }
            })


        }

        // Promise then方法的测试
        sendAjax('http://api.k780.com/?app=weather.today&weaId=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json').then((value) => {
            console.log(value);
        },(reason) => {
            
        })

        async function main(){
            //发送请求
            let result = await sendAjax('http://api.k780.com/?app=weather.today&weaId=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json');
            console.log(result);
        }
        main(); 
    </script>
</body>
</html>
posted @ 2021-09-22 17:19  成强  阅读(625)  评论(0编辑  收藏  举报