I'm fetching data from database with php and mysql and returning an array with json that looks like this in the console :
I'm trying to iterate through this result in the success callback with this code without success :
$.ajax({
type: 'POST',
url: 'query/getUpdate.php',
data: values,
dataType: 'json',
success: function(datas) {
if (datas.message === 'success') {
var selectedworkerid = datas.selectedworkerid;
//display number of new messages
datas = $.parseJSON(datas);
$.each(datas, function(key, val) {
console.log(key);
console.log(val.selectedworkerid);
});
}
}
});
What am I doing wrong or what I am missing for this to work?

if (datas.message === 'success') {is absolutely not trying to iterate...datasdoes not have amessageproperty: it is an array with objects. Those objects have themessageproperty.$.each(). You're trying to access elements too soon.dataType:'json'means your "datas" (should be "data") has already been parsed, so you don't need to re-parse it with$.parseJSON. Put the$.eachas the first line of the success callback and move the other code inside (and edit accordingly) - as others already said, you only have an array, "datas" itself doesn't have message/selectedworkerid.