Two Ajax requests in the same function

Asked

Viewed 503 times

1

When loading a page, I have two Ajax requests in sequence that fill two select s with options coming from different tables in the database.

But only the first is executed, the second does not even call. I already followed with debug, I put breakpoint to see if the second request enters and does not work.

Both are independent. Follow the code:

function Inicio(){
   // Primeira requisição que é executada
   $.get('ctrl/administrativo/modcadcon.ctrl.php',
           {'acao':'todos_atv', 'campo':'mod_status', 'valor':'1'},
           function(ret_mod){

           // Preenche o select com o resultado

           }
   );

   // Segunda requisição não é executada
   $.get('ctrl/administrativo/procadcon.ctrl.php',
           {'acao':'consultar', 'campo':'pro_status', 'valor':'1'},
           function(ret_pro){

           // Não chega aqui...!
           }
   );
}

[RESOLVED]

There was an error in the PHP script that received the second request... Now they both work.

  • 2

    Already put a console.log in the second to see if there is action?

  • 1

    Also note if there was a javascript error when running the page, because when this happens, the rest of the code that exists after the error does not run.

  • @sam e @Marcell Alves - the time that comes on the second line $.get..., following with debug, it does not run. There must be something wrong here. The php file exists... it is very strange that.

1 answer

0


Try it this way :

function Inicio(){
   // Primeira requisição que é executada
   $.get('ctrl/administrativo/modcadcon.ctrl.php',{'acao':'todos_atv', 'campo':'mod_status', 'valor':'1'})
         .done(function( data ) {
            console.log('Preenche o select com : ' + data)
            $.get('ctrl/administrativo/procadcon.ctrl.php',{'acao':'consultar', 'campo':'pro_status', 'valor':'1'})
               .done(function( data2 ) {
                  console.log('Preenche o select com : ' + data2)
               });
         });
  }
  • This solution is valid, but it took me a while to realize that the error was in the PHP script. Thank you so much for the help!

  • Even so I find it safer to wait for a requirement to finish before doing the second !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.