How to show a json with jQuery and PHP?

Asked

Viewed 234 times

-1

I did this ajax here, PS: the data reaches the.php data file.

$.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 cannot 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 doubt is, as I show it, there is a div called id="answer" but it gets nothing...

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.

  • So I tried but did not show the result on the screen, so as I said it even goes the answer, calculates, everything... Shows {"total_direta":"21.450,00","total_indireta":"0,00","total_geral":"47.500,00"} but does not return and prints on screen... :/

  • So instead of making $('#answer'). append(), switch to $('#answer'). text();

  • I edited my answer to make it clear.

  • is, no, also not printed the json result on the screen...

  • Dude, it would be nice if you put the whole code there then. It gives the Alert in the data, but doesn’t enter the #answer? Weird.

  • Try taking the line from: dataType: 'json'

  • So actually neither does the Alert it does, but the status of the answer is 200, so it should fall into the Success... But, I took out dataType: 'json', and still it wasn’t, nor the screen print... The.php Data Code here

  • /*Comissao direta*/ 
 $porcentagem = '1.43%';
 $casas_vend = $_POST['casa_vend'];
 $media_valor = $_POST['media_valor'];
 $total_direta = (($media_valor * $porcentagem) / 100) * $casas_vend ; 
 $data = array('total_direta'=> $total_direta,'total_indireta' => $total_indireta, 'total_geral' =>$total_geral);
 
 
 echo json_encode( $data );

  • The errors are in php. I made here and returned a number of errors, among them, the fact that you are making mention of the binary variables. And you were supposed to have edited the answer and put the code, not here in the comments. It got very confusing...

  • is that I took what didn’t really matter... But all these variables exist and I created them in one place... I’m going to put in Pastebin..

  • I got here, actually had 2 points... dataType that was missing, and had a method that did not exist called number_format in js, but I solved here the

Show 6 more comments

0

Try it this way

$.ajax({
    type: 'post',
    data: $(this).serialize(),
    dataType: 'json',
    url:'simulacao/dados.php',
    success: function(data){
        $('#resposta').append(data[0].total_direta);
    }
}); 
  • Thank you very much, this code would work, as I explained in the comment of the above answer, missing dataType and there was a method called number_format that doesn’t exist, I, for some reason, thought there was a native number formatting command in js, but very good, your code would work perfectly!

Browser other questions tagged

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