I have this ajax call which its result is in a json format
$.ajax({
url: link,
dataType: "json",
success: function(data){
console.log(data); //C.L-1
console.log(data.length); //C.L-2
}
})
And here is the result
$users_arr = array();
//After a successful query from my database
While($sql = mysqli_fetch_array($query)){
$user_id = $sql['id'];
$user_age = $sql['age'];
$user_name = $sql['name'];
$users_arr[$user_id] = $user_id.','.$user_name.','.$user_age;
}
echo json_encode($users_arr);
Now the C.L-1 returns true but the C.L-2 returns undefined. I thought the data
returned is an array on it's own so I want to run a for loop for each user like this
for(var i = 0; data.length > i; i++){
eachUser = data[i];
userInfo = eachUser.split(',');
userId = userInfo[0]; // and so on for the other info
$('div#age'+userId).html(userAge);
}
But the problem is that the data.length
returns undefined which prevents me for running my for loop. Please any better help?
Edit here are some data from C.L-1
Object {8: "8,debbie,23", 3: "3,doggie,20", 7: "7,dini,22"}