Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for assistance displaying returned JSON array 1

Status
Not open for further replies.

ochaos

Programmer
Sep 9, 2008
18
US
Hi there,

I am making an ajax request to a PHP page and returning the results as a JSON array.

Where I am having a hard time is grabbing the specific parts of the array to display on my page.

I'm using Chrome's javascript console and logging to the console, so I can see the returned array. Here is an example result.

Code:
{"0":{"id_member":"2","member_name":"User 1"},"1":{"id_member":"5","member_name":"User 2"},"2":{"id_member":"619","member_name":"User 3"},"status":100,"message":"Success!"}

In my JQuery postback success function, if I do the following, I see the above result.

Code:
function PostbackSuccess_ShowResults(_data, textStatus, XMLHttpRequest) {
    console.log(_data);

    $('#showData').html(_data);
    //alert("Postback Sucess");
}

but if I just want to show the member_name of user 1, I can't identify how to do that. I would think I could use _data.0.member_name, but that doesn't work and I've tried _data[0].member_name also with an undefined result also.

Any assistance or direction would be greatly appreciated.
 
Hi

ochaos said:
_data.0.member_name, but that doesn't work
0 is not a valid identifier in JavaScript, so you can not use it as property name.
ochaos said:
_data[0].member_name also with an undefined result also.
It is string '0', not numeric 0.
JavaScript:
_data['0'].member_name

[gray]// or[/gray]

_data['0']['member_name']
Of course, supposing that _data is an Object, not the string as it came from the server.

Feherke.
 
Thank you for the prompt reply. Here's the error logged in the console when I try this..

Code:
Uncaught TypeError: Object function (E,F){return new o.fn.init(E,F)} has no method 'parseJSON'
PostbackSuccess_ShowResultsfunctions.js:37
I
jquery.js:19

I will post my MySQL query that generates the JSON, thank you!
 
Hi

ochaos said:
Here's the error logged in the console when I try this..
Well, the error message without the erronous code is quite useless. Please post the code which does that work, both sending and receiving part of the AJAX call. ( But an URL to a publicly accessible copy of that page would be the best. )

Also tell us your browser and jQuery library version.
ochaos said:
I will post my MySQL query that generates the JSON
Completely indifferent for the current debugging.

Feherke.
 
Sorry for the short reply, I am doing this for a side project and am busy at work and trying to just get a reply to stay engaged.

Thank you for your prompt reply and again, sorry about my lack of detail.

I was testing this on a site I am upgrading and it was using an outdated JQuery library. I linked the script to a current library and it works now.

Thanks again for your assistance, I'm all set now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top