JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 13 of the JSON data

4

I have a problem with receiving a JSON file from a PHP to a Jquery:

  

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 13 of the JSON data

In my PHP I have:

<?php
if(isset($_POST['id_parte'])){
$lista_productos = lista_productos('',$conn);
for ($i=0; $i < $lista_productos['count']; $i++) {
    if ($_POST['id_parte'] == $lista_productos[$i]['id_parte']['id']) {
        if ($lista_productos[$i]['status']!=66) {
            $listado = array('id'=>$lista_productos[$i]['id']);
            // $listado = $lista_productos[$i];
            echo json_encode($listado, JSON_FORCE_OBJECT);
        }
    }

}
}

And the javascript is:

$.post(".carga_productos_filtro.php",{id_parte:id_parte},
    function(response){

        console.log(response);
        response = JSON.parse(response);


        $('#list_productos_'+id_parte).html("<h2>"+response+"</h2>");
    });

And I receive this in the console.log :

{"id":"108"}{"id":"109"}{"id":"110"}{"id":"113"}{"id":"114"}

I should receive more but for the moment if I manage to pass to json, I am satisfied, and afterwards I add more things to it.

What I do not understand is why the error since I have done something similar in the same way and it works but in this case no, I must add that in the other one that I have looked like in the console.log it looks like a array (and pink) instead in this it looks as if it were one more variable (and black) and then you can not parsed to JSON.

The errors in addition to my Script also give me in lines of Jquery

  

jquery-3.3.1.min.js: 2: 27452 ,

     

jquery-3.3.1.min.js: 2: 28202 ,

     

jquery-3.3.1.min.js: 2: 77649 ,

     

jquery-3.3.1.min.js: 2: 79907 ;

I already feel the case so long, I want to be clear from the beginning (add that I use the PHP Smarty framework).

    
asked by juank 09.08.2018 в 11:23
source

1 answer

4

The problem is that you are sending the browser several consecutive JSONs (one for each iteration of the loop) instead of a JSON with all the data you want to send. JSON consecutive do not form a valid JSON.

To do it correctly you must put all the data in a single variable and send it once only:

<?php
if (isset($_POST['id_parte'])) {
    /* Datos a ser enviados al navegador */
    $listado = [];
    $lista_productos = lista_productos('', $conn);
    for ($i = 0; $i < $lista_productos['count']; $i++) {
        if ($_POST['id_parte'] == $lista_productos[$i]['id_parte']['id']) {
            if ($lista_productos[$i]['status'] != 66) {
                /* Agregamos el elemento a la matriz de listados */
                $listado[] = array(
                    'id' => $lista_productos[$i]['id'],
                );
            }
        }
    }
    /* Enviamos todos los datos de manera correcta una única vez */
    echo json_encode($listado, JSON_FORCE_OBJECT);
}
    
answered by 09.08.2018 / 11:30
source