Good afternoon, please support, by inserting the data of a json in php to a datatable does not show me, I have validated and the data if they are loaded in the php but by pulling them to a datatable does not show them.
my getjson.php code
require_once 'dbconfig.php';
$posts = array();
$query = ("SELECT * FROM user");
$stmt = $db_con->prepare($query);
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
$posts['datos'][] = $row;
}
echo json_encode($posts);
This is the script that I have in my index.php pulling with e $ .ajax the data that I have the getjson.php, but I can not get them to be displayed.
$(document).ready(function(){
$.ajax({
url: 'getjson.php',
dataType: 'json',
})
$.each(data.datos, function(i,post){
var newRow =
"<tr>"
+"<td>"+post.login+"</td>"
+"<td>"+post.email+"</td>"
+"</tr>";
$(newRow).appendTo("#json-data tbody");
});
$('#json-data').DataTable();
});
I did some tests replacing the $ .ajax with temporary data and shows them without any problem.
$(document).ready(function(){
var data = {
'datos':
[
{'login':'si', 'email':'[email protected]'},
{'login':'si', 'email':'[email protected]'}
]
};
$.each(data.datos, function(i,post){
var newRow =
"<tr>"
+"<td>"+post.login+"</td>"
+"<td>"+post.email+"</td>"
+"</tr>";
$(newRow).appendTo("#json-data tbody");
});
$('#json-data').DataTable();
});
Your support indicating that I am wrong to pull and show the data of the getjson.php within the $ .ajax