Json in Jquery returns Undefined

Asked

Viewed 687 times

0

Although I have looked at all the various questions about it, I cannot make my json work, the code is like this:

$.ajax({
    url: '<?= base_url('login/Pesquisar'); ?>',
    type: 'POST',
    data: $("#formulario_pesquisa").serialize(),
    dataType: 'json',
    success: function(data){
        alert(JSON.stringify(data))
        alert(Object.keys(data));
        alert(data.email);
    }
}); 

The results are:

alert(JSON.stringify(data)) >>> {"cnpj ":"68.207.717/0001-42","email ":"[email protected]","Logado":true)

alert(Object.keys(data)); >> cnpj ,email ,Logado

alert(data.email); >> undefined

Can someone help me with that??

  • 1

    the problem should be exactly what Gabriel said. Only one to include also if Voce has no way to change the server can get the value like this data['email ']note that you have space after email

  • True, it could work this way too, but I had how to change yes, it was a slip even rsrs. Thank you very much/

1 answer

1


When using JSON.stringfy() it is typing {"email ":"value"}, see that there is a blank space after the "email" key. Check that the code that returns JSON is mounting it correctly.

Take an example:

This first will return Undefined because the key is defined as "email ".

var json = {"email ":"[email protected]"};
console.log(json.email) <= undefined

This will return the contents "[email protected]"

var json = {"email":"[email protected]"};
console.log(json.email) <= [email protected]
  • My, my, my, thank you very much

Browser other questions tagged

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