Search code examples
jqueryasp.net-mvcasp.net-ajaxgetjson

Can I call $.ajax method inside $.getJson()?


I want to insert value into database and with $.getJson() method I check if my value exist into database and if not I want to call $.ajax to put it into database I tried like this and modal dialog pops up but method continue with execution and I my application is broken every time how to prevent execution after modal dialog pops up I try to put $.ajax inside else block but nothing then I tried like this but nothing?

enter $("#btnSave").click(function (e) {
    e.preventDefault();

    var form = $(this).closest("#forma1");

    $.ajax({
        type: "POST",
        url: form.attr("Create"),

        data:   $.getJSON("/InspekcijskeKontrole/Check?id1=" + $("#kombo3").val() + "&id2=" + $("#kombo4").val(), function (data) {

            if (data.InspekcijskoTijeloId != -1 && data.ProizvodId != -1) {
                $.getJSON("/Proizvodi/VratiIme/" + data.ProizvodId, function (ime) {

                    if (ime != null) {
                        $("#modalni1 p").text("Inspekcijska kontrola za " + ime + " je vec izvrsena");
                        $("#modalni1").modal({ backdrop: "static" });

                    }
                });

            } 

        }),
        data:form.serialize(),

        success: function (response) {

         alert("Informacije su uspjesno ubacene");
         window.location.href = "/InspekcijskeKontrole/Index";


        },
        error: function (greska) {
            alert("Doslo je do greske pri ubacivanju");
        }

    });

});code here

Solution

  • This doesn't work because the two inner "Check" and "VratiMe" ajax calls finish after the post has already completed.

    Because they are Asynchronous (ie queue up and return immediately and then complete at a later date).

    You should be able to change the order of your calls to something like:

    var form = $(this).closest("#forma1");
    
    $.getJSON("/InspekcijskeKontrole/Check?id1=" + $("#kombo3").val() + "&id2=" + $("#kombo4").val(), function (data) {
    
        if (data.InspekcijskoTijeloId != -1 && data.ProizvodId != -1) {
            $.getJSON("/Proizvodi/VratiIme/" + data.ProizvodId, function (ime) {
    
                if (ime != null) {
                    $("#modalni1 p").text("Inspekcijska kontrola za " + ime + " je vec izvrsena");
                    $("#modalni1").modal({ backdrop: "static" });
                }
            });
        }
        else {
    
            $.ajax({
                type: "POST",
                url: form.attr("Create"),
    
                data:form.serialize(),
    
                success: function (response) {
    
                 alert("Informacije su uspjesno ubacene");
                 window.location.href = "/InspekcijskeKontrole/Index";
    
    
                },
                error: function (greska) {
                    alert("Doslo je do greske pri ubacivanju");
                }
    
            });
        } 
    });