MattBH
MattBH

Reputation: 1652

JSON object returning with values but displaying undefined

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]

Upvotes: 3

Views: 7632

Answers (1)

Kelly
Kelly

Reputation: 41591

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,

data = data[0];

Upvotes: 4

Related Questions