jQuery:执行同步 AJAX 请求后跟一系列其他 ajax 请求

jQuery: Performing synchronous AJAX request followed by a chain of other ajax requests

我有一个 ASP.NET MVC 应用程序,我正在尝试在 javascript 函数中使用 jQuery 一个接一个地创建一系列操作。该功能由三部分组成。

我想做的是:如果满足某些条件,那么我想执行同步 jQuery ajax 调用 CheckData。取决于返回的结果:

所以我设置了 async: false 但它不起作用,程序继续执行第 2 部分和第 3 部分。 我知道 async:false 已被弃用,我该如何实现?

function onCheckValidData()
{
    // do something....

    // PART 1 STARTS HERE

    if (some_condition_is_satified)
    {
        $.ajax({
            url: '@Url.Action("CheckData", "MyController")',
            async: false,
            type: "POST",
            dataType: "JSON",
            beforeSend: function () {
                showLoading();
            },
            success: function (result) {
                if (!result.isOk) {
                    return;
                }
            },
            complete: function(){
                hideLoading();
            }
        });
    }

    // PART 2 STARTS HERE

    // do something.....
    // continue doing more thing.....
    // more things.....

    // PART 3 STARTS HERE

    $.ajax({
        url: '@Url.Action("MyActionMethod1", "MyController")?' + paramsStr,
        type: "POST",
        dataType: "html",
        beforeSend: function () {
            showLoading();
        },
        success: function (result) {
            if (result == 'True') {
                jsMethod2();  // jsMethod2 is another javascript method which contains another $.ajax block
            }
            else if (result == 'False') {
                jsMethod3(); // jsMethod3 is another javascript method which contains another $.ajax block
            }
            else {
                alert(result);
            }
        },
        complete: function(){
            hideLoading();
        }
    });
}

我在控制器中的操作:

    private JsonResult CheckData()
    {
        MyBoolResult res = new MyBoolResult();

        // do something....

        return Json(new { isOk = res.isOk });
    }


    public String MyActionMethod1(String param1, String param2, bool param3, string param4, string param5)
    {

       // do something

       return condition ? "True" : "False";
    }

无需同步。如果您希望 "PART 2" 和 "PART 3" 等待 ajax-请求完成,只需将它们放入函数中并在成功时调用它们:

function onCheckValidData()
{
    // do something....

    // PART 1 STARTS HERE
    if (some_condition_is_satified)
    {
        $.ajax({
            url: '@Url.Action("CheckData", "MyController")',            
            type: "POST",
            dataType: "JSON",
            beforeSend: function () {
                showLoading();
            },
            //Success will execute only if the ajax-request is finised
            success: function (result) {
                if (!result.isOk) {
                    return;
                }
                part2(); 
                part3();
            },
            complete: function(){
                hideLoading();
            }
        });
    }

    // PART 2 STARTS HERE
    function part2 () {/*do something.....*/}

    // PART 3 STARTS HERE
    function part3 () {/*$.ajax({...})*/}
}