jQuery deferred object with nested ajax calls

后端 未结 3 2028
一向
一向 2020-12-28 17:35

I have a situation in which my ajax calls must perform in a particular order. I have used jQuery Deferred objects in other situations, but cannot seem to find a way to make

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 17:47

    It's actually quite simple. Though all the AJAX calls are Deferred objects, I still use one for the method itself.

    function nestedAjax() {
        var dfd = $.Deferred();
        $.get("/echo/json/", function(){
            console.log("First ajax done.");
            $.get("/echo/json/", function(){
                 console.log("Second ajax done.");
                dfd.resolve();
            });
        });
    
        return dfd.promise();
    };
    

提交回复
热议问题