jq的ajax中promise的應用


1.jq3.2.1版本,支持promise寫法

2.$ajax()返回的是一個promise對象

3.如果有多個ajax請求 ,可以使用es6中的promise.all方法

例子:

<script src="./jquery-3.2.1.js"></script>
<body>
    <script>
        var a = $.ajax({
            url: './data.txt',
            dataType: 'json',
        })

        var b = $.ajax({
            url:'./data2.txt',
            dataType:'json'
        })

        console.log(a);
        a.then(res=>{
            console.log(res);
        },err=>{
            console.log(err);
        })
        
        Promise.all(
            [a,b]
        ).then(res => {
             let [res1,res2] = res
             console.log(res1);
             console.log(res2);
        }, err => {
            alert('錯了')
        })
    </script>
</body>

 


免責聲明!

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



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