How to handle the JSON response with Javascript

Asked

Viewed 277 times

1

<script type="text/javascript">
$(function($) {
    $('#enviaadv').submit(function() {
        //document.getElementById('btnConsultar').disabled = true; 
        $('div.mensagem-erro').html('');

        $(this).ajaxSubmit(function(resposta) {

            if (!resposta){
                $onclick = showadv('bottom','left',resposta);
            }
            else
            {
                $onclick = showadv('bottom','left',resposta);           
            }

        });

        return false;

    });
});
</script>   

I’m receiving JSON data on resposta thus:

{"adv_nome":"Gabriel","adv_id":"8c7dac3ea415b863c8c8789b6667b2b1"}

How can I treat this data to get adv_nome and adv_id?

  • Imagine you want to play in one input, would be so: $('#id_do_input').val(resposta.adv_nome);

  • tried to do so $name.val(reply.adv_name); $onclick = showadv('bottom','left',name); did not give

1 answer

2


You can access the object values Json just use (.) for more examples: https://www.w3schools.com/js/js_json_objects.asp

var resposta = {"adv_nome":"Gabriel","adv_id":"8c7dac3ea415b863c8c8789b6667b2b1"};
var x = resposta.adv_nome;
document.getElementById("demo").innerHTML = x;
<p id="demo"></p>

  • ta dando Undefined

  • i must exchange json.name for reply.name

  • @That’s Joaopedropm. I made a change in response to match your code.

  • is giving Undefined

  • @Joaopedropm you used answer.adv_name ?

  • this is the exact answer that comes from PHP

  • {"adv_name":"Gabriel","adv_id":"8c7dac3ea415b863c8c8789b6667b2b1"}

Show 2 more comments

Browser other questions tagged

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