Home » Jquery » javascript – jQuery: Performing synchronous AJAX request followed by a chain of other ajax requests-Exceptionshub

javascript – jQuery: Performing synchronous AJAX request followed by a chain of other ajax requests-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have an ASP.NET MVC app and I am trying to make a chain of operations, one after another using jQuery inside a javascript function. The function consists of three parts.

What I am trying to do is: If some condition is satisfied then I want to execute syncrhonous jQuery ajax call CheckData. Dependending on the result returned:

  • It returns ok -> I want to continue executing part2 and finally part3 in this order.
  • It returns Nok -> then it finishes and returns. It does not continue executing part2 and part3.

So I have set async: false but it is not working, program continues executing part2 and part3.
I know async:false is deprecated so how can I achieve this?

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();
        }
    });
}

My actions in the controller:

    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";
    }
How to&Answer:

No need to make that synchornuous. If u want the “PART 2” and “PART 3” to wait for the ajax-request to finish just put them into a function and call them on success:

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({...})*/}
}