Invalid JSON response

0

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';
}else{
    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');

        $serverName='(LOCAL)';
        //$serverName=$_POST['bases'];
        $connectionInfo=array("Database"=>"dbsav300", "UID"=>"sa", "PWD"=>"admin01");
        $conn=sqlsrv_connect($serverName, $connectionInfo);

        if ($conn) {
            # code...
        }else{
            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
        $i=0;
        $tabla="";
        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'].'"},';
            $i++;
        }
        $tabla=substr($tabla,0, strlen($tabla) -1);
        echo '{"data":['.$tabla.']}';

    }   

? >

And the html as follows:

    <!DOCTYPE html>
<html lang="en">
<head>
    <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='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
    <title>Contenido</title>
    <link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
</head>
<body>
    <div class="menu-arriba">
        <ul>
            <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>
        </ul>
    </div>
    <div class="imagen-sana">
        <img src="imagenes/Sana.png" alt="logo">
    </div>
    <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">
        </form>'


   <table id="ejemplo" class="display" cellspacing="0", width="100%">
            <thead>
                <tr>
                    <td>SKU</td>
                    <td>COSTO</td>
                    <td>FECHA DE MOVIMIENTO</td>
                    <td>PIEZAS</td>
                    <td>TICKET</td>
                    <td>EXISTENCIA FINAL</td>
                    <td>USUARIO</td>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
    <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>
</body>
    <script type="text/javascript">
    $(document).ready(function() {
    $('#ejemplo').dataTable( {
        "ajax": "../sana_php/kardex.php",
        "columns":[
            {"data": "art_Clave"},
            {"data": "art_Costo"},
            {"data": "kar_Fecha"},
            {"data": "kar_Cantidad"},
            {"data": "kar_Origen"},
            {"data": "alma_Existencia"},
            {"data": "usu_Clave"}
        ]
    } );
    } );
    </script>
</html>
    
asked by Guillermo Ricardo Spindola Bri 28.07.2016 в 18:57
source

1 answer

1

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 ..

$i=0;
while($row=sqlsrv_fetch_array($resultado)){
    $data['data'][$i][] = $valor; 
    ......
    $i++;
} 

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
source