首页 > Web开发 > 详细

jq的ajax中promise的应用

时间:2019-10-30 01:33:59      阅读:97      评论:0      收藏:0      [点我收藏+]

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>

 

jq的ajax中promise的应用

原文:https://www.cnblogs.com/luguankun/p/11762554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!