Reputation:
I have some json data that coming from my php code here it is
{"Inboxunreadmessage":4,
"aaData":[{
"Inboxsubject":"Email SMTP Test",
"Inboxfrom":"Deepak Saini <*****@*****.co.in>"},
{"Inboxsubject":"Testing01",
"Inboxfrom":"Deepak Saini <**********@gmail.com>"}
]}
I'm fatching it like this
$(window).load(function(){
$.ajax({
dataType: "json",
type: "POST",
url: "/mail/mail-action",
data: {
_act: "load"
}
}).done(function(data) {
if(data.message){
showMessage(data);
}
//This is working
$('.Inboxunreadmessage').html(data.Inboxunreadmessage);
//This is Not working ----------------------
$('.Inboxsubject').html('<li><a href="#"><i class="fa fa-users text-aqua"></i> '+data.Inboxsubject+'</a></li>');
}).error(function(jqxhr, exception){
ajaxErrorHandler(jqxhr, exception);
});
});
I'm getting error in
$('.Inboxsubject').html('<li><a href="#"><i class="fa fa-users text-aqua"></i> '+data.Inboxsubject+'</a></li>');
Upvotes: 0
Views: 47
Reputation: 54
You are accessing the data with a wrong JSON path.
data.Inboxsubject
should be changed to data.aaData[0].Inboxsubject
. This will print the first Inboxsubject of the data.aaData
array.
Now since data.aaData
is an array, you could loop over it to print out all Inboxsubjects.
var blockhtml="";
for(var i=0;i<data.aaData.length;i++)
{
blockhtml= blockhtml+'<li><a href="#"><i class="fa fa-users text-aqua"></i> '+data.aaData[i]["Inboxsubject"]+'</a></li>'
}
$('.Inboxsubject').html(blockhtml);
Upvotes: 1
Reputation: 26143
The data you are receiving has an array with further data in it...
{
"Inboxunreadmessage":4,
"aaData":[{
"Inboxsubject":"Email SMTP Test",
"Inboxfrom":"Deepak Saini <*****@*****.co.in>"
}, {
"Inboxsubject":"Testing01",
"Inboxfrom":"Deepak Saini <**********@gmail.com>"
}]
}
So to get the first subject you would use...
data.aaData[0].Inboxsubject
to get the next one you would use...
data.aaData[1].Inboxsubject
To parse all the aaData
objects, you could use this...
data.aaData.forEach(function(aa) {
// here you have aa.Inboxsubject and aa.Inboxfrom
});
Upvotes: 1