$. post does not work

Asked

Viewed 119 times

0

I created a Javascript code to receive a JSON, but when the data arrives in the $.post the code stops being processed. I found that in php the JSON file is populated correctly before it is sent to Javascript, for this reason, I believe the problem is in $.post, because that’s where the debugger.

$(function(){

    carregar(0, 3, 'Chamadas/listarAnuncios.php');
    $(document).on('click', '#carregarMais',function(){  

        var init = (jQuery('.anunciosJson').length);
        carregar(init, 3, 'Chamadas/listarAnuncios.php')
    });
    function carregar(init, max, url){
        var dados = { init : init, max : max };
       if(init >= 3)
        {
            $('#img_loadBuscarAnuncio').fadeIn('slow');
        }
        $('#cardContainer').css("opacity", 0.4);

        $.post(url, dados,  function (data) {// o código para aqui   <----------
            $("#carregarMais").last().remove();
            $('#img_loadBuscarAnuncio').fadeOut('slow');
            $('#cardContainer').css("opacity", 1.0);

            for(i = 0; i < data.dados.length; i++){
                var imgTeste = data.dados[i].img ? data.dados[i].img : "../anuncio-padrao.png";

            $("#cardAnuncios").append('<div class="anunciosJson">'
                +'<a style="display: block; color: rgba(0,0,0,0.87);" href="#">'
                     +'<div style="box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12); overflow: hidden; margin-bottom: 6px;">'  
                         +'<div class="col s4 m4" style="padding: 0px; margin: 0px;">'
                            +'<div style="width: 100%; overflow: hidden;">'
                                +'<div style="display: inline-block; position: relative; right: -50%;">'
                                     +'<img src="img/anuncios/'+imgTeste+'" alt="user background" style="height: 150px; width: auto; position: relative; left: -50%; vertical-align: bottom;">'
                                 +'</div>'
                             +'</div>'
                         +'</div>'
                         +'<div class="col s8 m8 truncate-text" style="padding-left: 14px; padding-top: 8px; height: 150px;">'
                                 +'<span class="grey-text text-darken-4" style="font-size: 20px;">'+data.dados[i].nm_titulo+'</span>'
                                +'<br>'
                                +'<span class="grey-text">Anúncio criado por: '+data.dadosComplementares[i].nm_usuario+' em '+data.dados[i].dt_criacao+'</span>'
                                +'<div class="star-result" style="margin-bottom: -10px;">'
                                     +'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">'
                                     +'<style>'
                                         +'.checked {'
                                             +'color: orange;'
                                        +'}'
                                   +'</style>'
                                    +'<span class="fa fa-star checked"></span>'
                                     +'<span class="fa fa-star checked"></span>'
                                     +'<span class="fa fa-star checked"></span>'
                                     +'<span class="fa fa-star"></span>'
                                     +'<span class="fa fa-star"></span>'
                                 +'</div>'
                                 +'<br>'
                                 +'<i class="mdi-image-navigate-next cyan-text text-darken-2"></i>'
                                 +'<span class="cyan-text text-darken-2">Informática</span>'
                                 +'<br>'
                                 +'<i class="mdi-communication-location-on cyan-text text-darken-2"></i>'
                                +'<span class="cyan-text text-darken-2">Encruzilhada, Santos - São Paulo</span>'
                         +'</div>'
                     +'</div>'
                 +'</a>'
                 +'</div>');
            }
            console.info(data);
            $("#cardAnuncios").append('<button id="carregarMais" class="btn right" style="background-color: #0097a7;" type="submit" name="action"><center>Carregar mais</center></button>');
            $('#img_loadBuscarAnuncio').fadeOut('slow');
            $('#cardContainer').css("opacity", 1,0);
            var conta = $('<div class="anunciosJson">').length;

            if(init == max)
            {
            $("#carregarMais").last().remove();
            $('#img_loadBuscarAnuncio').fadeOut('slow');
            $('#cardContainer').css("opacity", 1.0);
            }

            if(conta == data.totalResults) {
                $("#carregarMais").hide();
            }

        }, "json");
    }

});
  • Is your Url being passed correct? From an error?

  • ta yes, in the case the Arry is like $result = array( 'totalAnunctions' => 0, 'data' =>null, 'dataComplementares'=>null );

  • if I take that part $result = array( 'totalAnuncios' => 0, 'data' =>null ); it works

  • only that the strange thing is that there is no problem in all php, json is populated correctly

1 answer

0


I managed to solve the problem was that before sending the data to javascript I was not converting the characters to utf8, I did it in php and the code worked

Browser other questions tagged

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