admin管理员组文章数量:1278985
When I call this code:
console.log(data);
console.log(data.email);
I get this result:
{"userName":"2","email":"2","firstName":"2","lastName":"2","isAdmin":"0","isEnabled":"1"} index.php:162
undefined
The first console.log(data);
outputs correctly. Then, I want to access the email
property of the data
object, and to do so I use console.log(data.email);
. However, as you can see above, it says that it's, "undefined."
Why can't I access this property (or any properties)? Note: I have also tried data['email']
which didn't work, either.
When I call this code:
console.log(data);
console.log(data.email);
I get this result:
{"userName":"2","email":"2","firstName":"2","lastName":"2","isAdmin":"0","isEnabled":"1"} index.php:162
undefined
The first console.log(data);
outputs correctly. Then, I want to access the email
property of the data
object, and to do so I use console.log(data.email);
. However, as you can see above, it says that it's, "undefined."
Why can't I access this property (or any properties)? Note: I have also tried data['email']
which didn't work, either.
3 Answers
Reset to default 7I didn't realize that jQuery doesn't auto-parse the returned JSON to an object. It was just a JSON string. To fix, I just had to do this:
data = JSON.parse(data);
Cross-browser:
data = $.parseJSON(data);
In my case, I could show the object by console.log, but I couldn't access any attribute for a simple thing: I wasn't realizing that my JSON was wrongly structured, as an array with one single object. Then I could access its attribute using a index.
console.log(data.email);
undefined
but then I tried...
console.log(data[0].email);
[email protected]
I just fixed my model and it works perfectly. Thanks to my coleague Aaron Lil!
I have experienced the same issue. And to have it fixed, I needed to create new array and then put each values of the data object in that array by iterating "data" like the following:
var arrayTest = [];
$.each(data, function (index, value) {
arrayTest["email"] = value.email;
arrayTest["andsoforth"] = value.andsoforth;
//etc...
});
// Now retrieve data by using arrayTest
console.log(arrayTest["email"]);
本文标签: javascriptCan39t access object properties in jQuery AJAX callStack Overflow
版权声明:本文标题:javascript - Can't access object properties in jQuery AJAX call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741230726a2362060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论