Invalid JSON response


I'm using Datatables and showing the data from an HTML table works perfectly, but when I want to show SQL Server data from PHP I get the following error:

My PHP code works in the following way:

    <?php session_start(); 

if (isset($_SESSION['usuario'])) {
    require 'vistas/kardex.view.php';
    header('location: servicios.php');
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $buscar=filter_var($_POST['buscar'], FILTER_SANITIZE_STRING);
            $fecha1=date_create_from_format('d-m-Y', $_POST['fecha1']);
            $formato1=date_format($fecha1, 'd-m-Y');    
            $fecha2=date_create_from_format('d-m-Y', $_POST['fecha2']);
            $formato2=date_format($fecha2, 'd-m-Y');

        $connectionInfo=array("Database"=>"dbsav300", "UID"=>"sa", "PWD"=>"admin01");
        $conn=sqlsrv_connect($serverName, $connectionInfo);

        if ($conn) {
            # code...
            echo "Conexion no pudo establecerse";
            die(print_r(sqlsrv_errors(), true));

        $query="SELECT art_Clave, art_Costo, kar_Fecha, kar_Cantidad, kar_Origen, alma_Existencia, usu_Clave FROM tArticuloKardex WHERE art_Clave='".$buscar."' AND (kar_Fecha BETWEEN '".$formato1."' AND '".$formato2."') ORDER BY kar_Fecha";

        $resultado=sqlsrv_query($conn, $query);

        //se guardan en un array multidimensional los datos de la consulta
        while ($row=sqlsrv_fetch_array($resultado)) {
            $tabla.='{"art_Clave":"'.$row['art_Clave'].'", "art_Costo":"'.$row['art_Costo'].'", "kar_Fecha":"'.date_format($row['kar_Fecha'], 'd-m-Y').'", "kar_Cantidad":"'.$row['kar_Cantidad'].'", "kar_Origen":"'.$row['kar_Origen'].'", "alma_Existencia":"'.$row['alma_Existencia'].'", "usu_Clave":"'.$row['usu_Clave'].'"},';
        $tabla=substr($tabla,0, strlen($tabla) -1);
        echo '{"data":['.$tabla.']}';


? >

And the html as follows:

    <!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/estilos.css">
    <link rel="stylesheet" href="fonts/style.css">
    <link href='' rel='stylesheet' type='text/css'>
    <link href="" rel="stylesheet"/>
    <script src=""></script>
    <script src=""></script>
    <div class="menu-arriba">
            <a href="index.php">INICIO</a>
            <a href="articulo.php">ARTICULOS</a>
            <a href="kardex.php">ALMACENES</a>
            <a href="ventas.php">VENTAS</a>
            <a href="pedidos.php">PEDIDOS</a>
    <div class="imagen-sana">
        <img src="imagenes/Sana.png" alt="logo">
    <div class="buscar">
        <a href="cerrar.php">Cerrar sesión</a><br>
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" name="buscar">
            <input type="text" name="buscar" id="buscar" placeholder="Buscar:">
            <input type="text" name="fecha1" id="fecha1">
            <input type="text" name="fecha2" id="fecha2">
            <input type="submit" name="enviar" value="Buscar">

   <table id="ejemplo" class="display" cellspacing="0", width="100%">
                    <td>FECHA DE MOVIMIENTO</td>
                    <td>EXISTENCIA FINAL</td>
    <div class="footer">
        <p>Todos los derechos reservados - 2016 &copy Farmacias Sana Sana S.A de C.V - <a href="">Términos y condiciones</a></p>
    <script type="text/javascript">
    $(document).ready(function() {
    $('#ejemplo').dataTable( {
        "ajax": "../sana_php/kardex.php",
            {"data": "art_Clave"},
            {"data": "art_Costo"},
            {"data": "kar_Fecha"},
            {"data": "kar_Cantidad"},
            {"data": "kar_Origen"},
            {"data": "alma_Existencia"},
            {"data": "usu_Clave"}
    } );
    } );
asked by Guillermo Ricardo Spindola Bri 28.07.2016 в 18:57

1 answer


You are returning the table and not the information, also wait for the information as json ..

What you can do is put together an array ..

    $data['data'][$i][] = $valor; 

and then with the following statement you return the values:

echo json_encode($data);

I leave a link for you to be an example:

Dinamyc data table with json

answered by 28.07.2016 / 20:26