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>

猜你喜欢

转载自www.cnblogs.com/luguankun/p/11762554.html
今日推荐