I cant get any data from Ajax with Json
I cant get any data from Ajax with Json
karel89
Posts: 1Questions: 1Answers: 0
My PHP code is:
<?php
include_once("conexion.class.php");
require("egresados.class.php");
$objEgresados=new Egresados;
$result=$objEgresados->show_egresados();
$jsondata = array();
while($row = mysql_fetch_array($result))
{
$cuenta=$row['cuenta'];
$nombre=$row['nombre'];
$apellido=$row['apellido'];
$tel1=$row['tel1'];
$tel2=$row['tel2'];
$email=$row['email'];
$carrera=$row['carrera'];
$campus=$row['campus'];
$jsondata[] = array('cuenta'=> $cuenta, 'nombre'=> $nombre, 'apellido'=> $apellido, 'tel1'=> $tel1, 'tel2'=> $tel2, 'email'=> $email, 'carrera'=> $carrera, 'campus'=> $campus);
}
$json_string = json_encode($jsondata);
echo $json_string;
<?php
>
```
and to call this I use this in the table:
?>
```js
$(document).ready(function() {
$('#dataTables-example').DataTable( {
data:"../datalayer/getegresados2.php",
columns:
[
{ title: "cuenta" },
{ title: "nombre" },
{ title: "apellido" },
{ title: "tel1" },
{ title: "tel2" },
{ title: "email" },
{ title: "carrera" },
{ title: "campus" },
{ data: "campus" }
]
});
});
I get the table columns but the data I only get "Loading"
This discussion has been closed.
Answers
Firstly you are setting the
data
option to use a string, you should be using theajax
option as this will actually retrieve the data for you.On the serverside you are using a flat array of objects, datatables expects any JSON to be returned from the server wrapped in a
data:
object. There are two ways to fix this, either add your $jsondata inside a thedata:
object like so,or just point
ajax.dataSrc
to be an empty stringThanks
Tom