Jquery以错误的顺序执行

SƲm*_* Aƥ 2 ajax jquery console.log

我的样本如下.我总是在"a"和"b"之前得到"c".如何相应地获得"a","b"和"c"?我很感激任何建议.

b.extend({
        get: function (id) {

        jQuery.ajax({
            type: 'GET',
            url: url,
            data: pdata,
            success: function (result) {
                console.log("a");
            }
        });

        for (var a = 0; a < 5; a++) {
            jQuery.ajax({
                type: 'GET',
                url: url,
                data: pdata,
                success: function (result) {
                    console.log("b");
                }
            });
        }
        console.log("c");
        }
});
Run Code Online (Sandbox Code Playgroud)

Tus*_*har 6

尝试

把你的代码放进去 success:

b.extend({
    get: function (id) {
        jQuery.ajax({
            type: 'GET',
            url: url,
            data: pdata,
            success: function (result) {
                console.log("a");
                for (var a = 0; a < 5; a++) {
                    jQuery.ajax({
                        type: 'GET',
                        url: url,
                        data: pdata,
                        success: function (result) {
                            console.log("b");
                            if (a === 5) {
                                console.log("c");
                            }
                        }
                    });
                }
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)