List results with jquery/php

Asked

Viewed 63 times

0

I’m looking to list results from a PHP page using jquery, which is updated every 3 seconds, but I’m not getting it. 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 in PHP ;)

  • 1

    What is going wrong?

  • No PHP output shown.

  • I included in the post an excerpt that I forgot

  • 1

    Do it inside the Success console.log(dados), and see if any results are coming...

  • 3

    Would not be json_encode that you should be using?

  • 1

    What is the status of the request, 200, 500? You only have the successful callback, put the error too, it may be having trouble parsing..

  • I really switched to json_encode and it worked. I’m sorry for the lack of attention and thank you all.

Show 2 more comments

1 answer

2


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

To correction was to exchange the method for json_encode.

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

Browser other questions tagged

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