JQuery getJSon nested calls do not work

I need to make two AJAX calls using the jQuery function getJSON.

The fact is that after making one of the calls, I need to immediately call the second function getJSON(because I need the result of the first call as a parameter in the second call).

My code is as follows:

$.getJSON("/Traslados/ObtenerCedulas", { Param1: $("#Param1").val(), Param2: $("#Param2").val() }, function (j) {
      $("#Result1").val(j); 
      $.getJSON("/Traslados/ObtenerLogins", { Param3: $("#Param3").val(), Param4: $("#Result1").val() },  
                function (k) {
                        alert(k);
                        $("#Result2").val(k);
       });
 });

The problem is that although the second call is executed correctly and it gives the expected result (I debugged it using the Firebug console), it does not raise a warning and even worse, it does not set the value kfor my Result2.

What's happening?

+3
source share
1 answer

, , , getJSON , , , , , # Result1.

API getJSON: http://api.jquery.com/jQuery.getJSON/ , getJSON, // .

, . ajax-, , .

<html>
<header>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script>
  function fakey_not_ajax(message, callback) {
    alert (message);
    callback();
  }
  function queue_my_stuff( kick_it_off_callback ) {
    $(document).queue("mydemo_queue", function(){
      fakey_not_ajax("show me first", function() { 
        $(document).dequeue("mydemo_queue");
      });
    });
    $(document).queue("mydemo_queue", function(){
      fakey_not_ajax("show me second", function() { 
        $(document).dequeue("mydemo_queue");
      });
    });

    kick_it_off_callback();
  }
  function push_the_button() {
    alert ("pushed");
    queue_my_stuff(
      function(){ 
        $(document).dequeue("mydemo_queue");
      }
    );
  }
</script>
</header>
<body>
Try this
<a href="#" onclick="push_the_button();">Push Me</a>
</body>
</html>
0

Source: https://habr.com/ru/post/1786692/


All Articles