Calling an ajax inside the sucess of another ajax contained in the first ajax

Asked

Viewed 28 times

0

At the click of a class button cantor is returned the singer’s name and buttons with class musica with their respective song names (data). By clicking a class button musica is returned to the lyrics of the song (data2), at that point I would like to add a button to trigger the first ajax (classe cantor) to relate the singer and their respective songs again. I made an attempt as can be seen in the comments of the script below, but it does not work. Is there any way to perform this operation?

$('.cantor').click(function() {
 
    $th = $(this);
      $.ajax({
        url: "pagina1.php",
        type: 'GET',
        data: {cantor: $th.attr( "title" )},
        success: function(data){
            $("#resultados").html(data);

                        $('.musica').click(function() {
                          $thm = $(this);
                          $.ajax({
                            url: "pagina2.php",
                            type: 'GET',
                            data: {identificador: $thm.attr( "title" )},
                            success: function(data2){
                            
                            
                                /*########### tentativa coloquei um button class cantor - não funciona ###### */
                                $("#resultados").html('<button class="cantor" title="Fulano de Tal">Fulano de Tal</button>'+data2);
                                
                                
                                
                            },
                            error: function(){
                                $("#resultados").html("Ouve um erro ao enviar sua URL");
                            }
                         });//ajax 
                    });

            
        },
        error: function(){
            $("#resultados").html("Ouve um erro ao enviar sua URL");
        }
     });//ajax 
});
  • The solution goes through Friend @Barmar in this post https://stackoverflow.com/questions/65727230/calling-an-ajax-within-the-success-of-another-ajax/65727449#65727449

1 answer

1

Would that be?

$('.musica').click(function (e) {
    e.preventDefault();

    $thm = $(this);
    $.ajax({
        url: "pagina2.php",
        type: 'GET',
        data: { identificador: $thm.attr("title") },
        success: function (data2) {

            /*########### tentativa coloquei um button class cantor - não funciona ###### */
            $("#resultados").html('<button class="cantor" title="Fulano de Tal">Fulano de Tal</button>' + data2);

        },
        error: function () {
            $("#resultados").html("Ouve um erro ao enviar sua URL");
        }
    });//ajax 
});

$('.cantor').click(function (e) {
    e.preventDefault();
    
    $th = $(this);
    $.ajax({
        url: "pagina1.php",
        type: 'GET',
        data: { cantor: $th.attr("title") },
        success: function (data) {
            $("#resultados").html(data);

            // Chama o evento de clique
            $('.musica').trigger('click');


        },
        error: function () {
            $("#resultados").html("Ouve um erro ao enviar sua URL");
        }
    });//ajax 
});

  • You’re not going back to the songs because of that line. <a href="https://musicasitalianas.com/index-m.php"><img border="0" src="https://musicasitalianas.com/images/back.gif"></a> this tag is making a redirect to the page, then it reloads the initial values.

Browser other questions tagged

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