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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM