How to call an ajax inside another?

1

I need to call another ajax request within a condition. But the outside ajax does not wait for the inside to finish. Example: I use this

 $.ajax({
                        type: "GET",
                        url: "MINHA_URL",
                        success: function (e) {
                        if(e == 'erro'){
                           $.ajax({
                               type: "GET",
                               url: "MINHA_SEGUNDA_URL",
                               success: function (f) {
                                  IMPRIMIE ALGUMA COISA
                               }
                       .....

The problem is that it does not execute correctly. How to proceed?

    
asked by anonymous 05.10.2016 / 14:00

2 answers

0
    $.ajax({
    url: 'MINHA_URL',
    type: 'post',
    data: {
        html: 'Teste 1'
    },
    success: function(returnhtml) {
        $("#result").append($('</p>').html(returnhtml));
        $.ajax({
            url: 'MINHA_URL',
            type: 'post',
            data: {
                html: 'Teste 2'
            },
            success: function(returnhtml) {
                $("#result").append($('</p>').html(returnhtml));
            }
        });

    }
}); 

Resultado : <div id="result"><p>Teste 1</p> <p>Teste 2</p> </div>

Practical example: link

    
05.10.2016 / 16:39
0
Mas o ajax de fora não espera o de dentro terminar para finalizar. 

Do not wait because they are asynchronous requests. Set in the ajax call the option async: false

    
05.10.2016 / 15:13