Reputation: 2274
I have a pretty basic problem but I can't seem to figure it out...
This is my ajaxcall via jquery:
function noname(){
$.ajax({
type : 'POST',
url : 'somefile.php',
dataType : 'json',
success : function(data){
$('#user').text(data.info.name).fadeIn(500);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
}
});
}
Let's say somefile.php outputs this:
{"proceed":"1","info":{"name":"John","online":"Online","id":"1"}}
In this case, everything works fine, and the user-div appears, containing the text "John".
However, let's say somefile.php outputs this:
{"proceed":"1","info":[{"id":"1","name":"John"},{"id":"2","name":"Rick"},{"id":"3","name":"Jane"},{"id":"4","name":"Astrid"}]}
To visualize it, here's the print_r of the array:
Array
(
[proceed] => 1
[info] => Array
(
[0] => Array
(
[id] => 1
[name] => John
)
[1] => Array
(
[id] => 2
[name] => Rick
)
[2] => Array
(
[id] => 3
[name] => Jane
)
[3] => Array
(
[id] => 4
[name] => Astrid
)
)
)
I want to output the same thing as I did in the working scenario, so I would put this in the ajax-call:
$('#user').text(data.info.0.name).fadeIn(500);
This gives an error, since I can't use numbers there. Anyone knows how to handle this?
Thanks a lot!!!
Upvotes: 2
Views: 90
Reputation: 26730
Since "info" is an array, you can simply access its entries like this:
data.info[0].name
Works the same for objects:
data['info'][0].name
Upvotes: 6