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


i want insert value database , $.getjson() method check if value exist database , if not want call $.ajax put database tried , modal dialog pops method continue execution , application broken every time how prevent execution after modal dialog pops try put $.ajax inside else block nothing tried 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 greske pri ubacivanju");         }      });  });code here 

this doesn't work because 2 inner "check" , "vratime" ajax calls finish after post has completed.

because asynchronous (ie queue , return , complete @ later date).

you should able change order of calls 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 greske pri ubacivanju");             }          });     }  }); 

Comments