3

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]

8
  • 1
    Can you post the JSON response as Firebug sees it? It might help us diagnose the problem. Commented Apr 1, 2011 at 12:24
  • What are you returning from server? How are you accessing object's properties? Please provide some code. Commented Apr 1, 2011 at 12:24
  • What does the console.log(data) show you? Commented Apr 1, 2011 at 12:24
  • Is your child appending a couple rows and showing "undefined" where you're accessing data? Commented Apr 1, 2011 at 12:41
  • yes the child is appending rows but showing "undefined" when accessing data Commented Apr 1, 2011 at 12:43

1 Answer 1

4

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];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.