Json in Jquery returns undefined

0

Although I have looked at all the various questions about this, I can not get my json to work, the code looks 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 anyone help me with this ??

    
asked by anonymous 11.08.2016 / 15:58

1 answer

1

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

See an example:

This first wrath will return undefined as the key is set to "email".

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

This will return the content "[email protected]"

var json = {"email":"[email protected]"};
console.log(json.email) <= [email protected]
    
11.08.2016 / 16:24