need example of manipulating data returned by ajax
need example of manipulating data returned by ajax
I want to make an ajax call to a .php page and then use javascript to manipulate the data to create a new json object that is used to populate the table. Basically the rows returned will become columns in the table, along with other derived columns.
Here is some js that I started just so I could see what is being returned by the php.
$('#incomeTable').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "/RetirePlan/getAccountsW.php",
"data": function(d) {
console.log(d);
d.year = 2018;
console.log(d);
}
},
"columns": [
{ "data": "Title" },
{ "data": "Description" },
{ "data": "AccountType" },
{ "data": "Value" },
{ "data": "GrowthRate" },
{ "data": "Owner" }
]
});
When I look at the output in the console, I don't see the data. After I add in d.year then I see that data, but not the data from the php. How do I access the php data and manipulate it before using it in the table?
This question has accepted answers - jump to:
Answers
Its a bit misleading but data in your ajax is what is being sent to the server, not what was received from the server.
If you want to see what is coming from the server, add
"dataFilter": function(d){console.log(d);
return d
;},
Hi @tekknow ,
Yep, as @bindrid said, the
data
is for the outgoing request,ajax.dataSrc
is for the incoming response. If you take a nose at thisajax
, there's a good example of what you want under the heading "Manipulate the data returned from the server".Cheers,
Colin
Thanks bindrid and colin. I had seen those examples but it hadn't sunk in. Then I found a better example googling somewhere. Now I get it.
Here is the link to the better example in case others have the same problem