1

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
jhony3
  • 272
  • 1
  • 2
  • 13
  • but why are you using two `getJSON` requestes? – mmushtaq Aug 01 '16 at 13:13
  • One to check if object exist and one to get the name of object if exist for showing information on modal dialog – jhony3 Aug 01 '16 at 13:16
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – freedomn-m Aug 01 '16 at 13:16

2 Answers2

1

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

        });
    } 
});
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • I tried like this but nothing works I am stucked for a long time then I got error for __RequstVerificationToken is not present but token is include inside serialize(); – jhony3 Aug 01 '16 at 13:16
  • Made an update and moved the else based on your additional details in the question and comments – freedomn-m Aug 01 '16 at 13:17
  • So the question is: Does your form submit *without* the check calls? Sounds like it doesn't, which is a completely different problem and nothing to do with ajax calls. – freedomn-m Aug 01 '16 at 13:26
  • Hey man it works now like this but I tried I swear on this way now it works ? – jhony3 Aug 01 '16 at 13:41
  • Error was because `var form=$(this).closest("form");` I called in ajax method – jhony3 Aug 01 '16 at 13:50
  • Yes, `this` changes a lot, depending on the scope. – freedomn-m Aug 01 '16 at 14:02
1

Why not do the check and possible insert in one request? I would strongly prefer to do it in one request!

But okay, that wasn't the question. To use the result of $.getJSON in $.ajax put it in the success callback of $.getJSON. Like this:

$.getJSON("getJsonUrl.html", function(responseOne) {
    $.getJSON("anotherJsonRequest.html", function(responseTwo) {
        $.ajax({
            url: "ajaxUrl.html",
            type: "POST",
            data = responseTwo
        });
    });
});
eisbehr
  • 12,243
  • 7
  • 38
  • 63