List results with jquery / php

0

I want to list results of a PHP page using jquery, which is updated every 3 seconds, but I can not. The code I'm using is:

<div id="listar"></div>

<script src="assets/js/jquery-3.1.1.min.js"></script>       

<script type="text/javascript">
function mostrar(){  
    $(document).ready(function(){       
             $.ajax({
                    type:'post',
                    dataType: 'json',
                    url: 'atualizar.php',
                    success: function(dados){
                    $('#listar').append(dados[0]);                         
                    }
            });
    });
}
//setInterval(function(){ mostrar; }, 3000);
setInterval(mostrar, 3000);
</script>

As an example, I put the PHP code below:

$ver = "teste";
echo json_decode($ver);

The problem is in Jquery and not PHP;)

    
asked by anonymous 15.12.2017 / 13:08

1 answer

2

The response type in the ajax request is asking for a json object, however the code in php is doing the opposite: encoding a json for an object through json_decode .

The fix has been to change the method to json_encode .

$ver = "teste";
echo json_encode($ver); // codifica a variável para json e retorna via echo
    
15.12.2017 / 13:26