How to show json with jQuery and PHP?

-1

I made this ajax here, PS: the data arrives until the file data.php

$.ajax({
          type: 'post',
          data: $( this ).serialize(),
          dataType: 'json',
          url:'simulacao/dados.php',
          success: function(data){
              alert(data);
            var objeto = data;
            $('#resposta').append(data.total_direta);;  
            }
        });  

However, I can not show the values on the screen ... The result of json is this

{"total_direta":"21.450,00","total_indireta":"0,00","total_geral":"47.500,00"}

My question is, how do I show this, there is a div called id="response" but it does not get anything ...

    
asked by anonymous 26.03.2017 / 20:41

2 answers

0

Try this:

$.ajax({
          type: 'post',
          data: $( this ).serialize(),
          dataType: 'json',
          url:'simulacao/dados.php',
          success: function(data){
              alert(data);
            // passa a resposta de string para JSON
            var resposta = JSON.parse(data);
            var objeto = data;
            $('#resposta').text(resposta.total_direta); 
            }
});  

I simply gave a JSON.parse () in the answer. Sometimes the answer is interpreted as string, so I do this and it works for me.

    
26.03.2017 / 21:00
0

Please try as follows

$.ajax({
    type: 'post',
    data: $(this).serialize(),
    dataType: 'json',
    url:'simulacao/dados.php',
    success: function(data){
        $('#resposta').append(data[0].total_direta);
    }
}); 
    
27.03.2017 / 14:45