JSON 对象返回值但显示未定义
我从数据库中选择数据并将其返回到 json 对象中,但是当我尝试访问对象的属性时,它们显示未定义,但是如果我使用 Mozilla 的 firebug 或内置于 Web 开发工具中的 Chrome 查看返回的对象,对象的属性具有值,
$.getJSON("info.php", {uid:one}, function(data){
var size = $(data).size();
console.log(data);
//display systems
$(child).append('<tr><th height="20" colspan="2">Contact Person</th><th height="20">Contact Number</th><th height="20" colspan="2">E-mail</th></tr><tr><td colspan="2">'+data.contact_person+'</td><td>0'+data.contact_number+'</td><td colspan="2">'+data.email_address+'</td></tr>');
child.show();
loader.hide();
});
非常感谢任何帮助。
这是 chrom 显示它的方式(我已将敏感数据替换为“-----”):
[ 对象
蜜蜂:“未定义”
bwo:“未定义”
手机号码:“---”
城市:“---”
公司名称:“----”
联系电话:“----”
联系人:“”
日期: "2010-10-18"
电子邮件地址:“-----”
esco_number:“--------”
传真号码:“0”
fgn:“未定义”
磅:“未定义”
无:“未定义”
号码:“761”
其他:“未定义”
邮箱:“------”
邮政城市:“------”
邮政编码:“-----”
邮政编码:“-----”
邮政省:“---------”
邮政_郊区:“--------”
省份:“-----”
与物理相同:“”
smee:“未定义”
状态:“活动”
街道:“--------”
郊区:“----------”
时间:“11:36:21”
uid:“----------”
网站:“”
原型:对象
]
// 尝试访问 chrome 控制台中的数据
对象.pobox
未定义
// 这里是 console.dir:
数组[1]
0:对象
蜜蜂:“”
bwo:“”
手机号码:“123456789”
城市:“巴瑟斯特”
公司名称:“测试”
联系电话:“123456789”
contact_person:“测试”
日期: "2011-03-22"
email_address: "[电子邮件受保护]"
esco_number:“010101”
传真号码:“123456789”
fgn:“”
磅:“”
无:“”
号码:“01”
其他:“”
邮箱:“0”
邮政城市:“”
邮政编码:“0”
邮政编码:“0”
邮政省份:“”
邮政郊区:“”
省份:“东开普省”
Same_as_physical:“开启”
史密:“”
状态:“活动”
街道:“测试”
郊区:“测试”
时间:“10:09:04”
用户 ID:“0”
网站:“test.co.za”
原型:对象
长度:1
原型:数组[0]
I'm selecting data from a database and returning it in a json object, but when i try to access the object's properties they display undefined, but if i look at the object being returned, either using Mozilla's firebug or Chromes built in web dev tools, the object's properties have values,
$.getJSON("info.php", {uid:one}, function(data){
var size = $(data).size();
console.log(data);
//display systems
$(child).append('<tr><th height="20" colspan="2">Contact Person</th><th height="20">Contact Number</th><th height="20" colspan="2">E-mail</th></tr><tr><td colspan="2">'+data.contact_person+'</td><td>0'+data.contact_number+'</td><td colspan="2">'+data.email_address+'</td></tr>');
child.show();
loader.hide();
});
Any help is much appreciated.
Here's how chrom displays it (i've replaced sensitive data with "-----"):
[
Object
bee: "undefined"
bwo: "undefined"
cell_number: "---"
city: "---"
company_name: "----"
contact_number: "----"
contact_person: ""
date: "2010-10-18"
email_address: "-----"
esco_number: "-------"
fax_number: "0"
fgn: "undefined"
lbs: "undefined"
none: "undefined"
number: "761"
other: "undefined"
pobox: "------"
postal_city: "------"
postal_code: "-----"
postal_postal_code: "-----"
postal_province: "---------"
postal_suburb: "--------"
province: "-------"
same_as_physical: ""
smee: "undefined"
status: "Active"
street: "--------"
suburb: "----------"
time: "11:36:21"
uid: "----------"
website: ""
proto: Object
]
// trying to access the data in chrome's console
Object.pobox
undefined
// here's console.dir:
Array[1]
0: Object
bee: ""
bwo: ""
cell_number: "123456789"
city: "Bathurst"
company_name: "test"
contact_number: "123456789"
contact_person: "test"
date: "2011-03-22"
email_address: "[email protected]"
esco_number: "010101"
fax_number: "123456789"
fgn: ""
lbs: ""
none: ""
number: "01"
other: ""
pobox: "0"
postal_city: ""
postal_code: "0"
postal_postal_code: "0"
postal_province: ""
postal_suburb: ""
province: "Eastern Cape"
same_as_physical: "on"
smee: ""
status: "Active"
street: "test"
suburb: "test"
time: "10:09:04"
uid: "0"
website: "test.co.za"
proto: Object
length: 1
proto: Array[0]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊,看起来你正在得到一个数组,其中包含一个对象。您可以在 PHP 脚本中修复它,也可以在 javascript 函数的开头添加一行修复,
Ahh, looks like you're getting an array with an object in it. You can either fix it in your PHP script or just add a one-line fix at the beginning of the javascript function,