Failed to return JSON: "Unexpected token in JSON at position 0" [closed]

0

I execute the SQL query and I receive the array processed with fetch_assoc . Finally in php I return it through ajax with json_encode .

echo json_encode($this->rows);

And this would be the code in JS:

function search(){ //recojo los datos y llamo a askDatabase
    var id_vehicle = document.buscar.id_vehicle.value;
    var brand = document.buscar.brand.value;

    askDatabase(id_vehicle, brand);
}

function askDatabase(id_vehicle, brand){ //Comunicación con PHP

    __ajax("controller.php?p=list/", "id_vehicle="+id_vehicle+"&brand="+brand)
    .done( function( info ){
        console.log("DATA: " + info);
        info = JSON.parse(info); //AQUI EL ERROR
        renderData(info);
    });
}

function __ajax(url, data){ //Ajax configured
    var ajax = $.ajax({
        "method": "POST",
        "url": url,
        "data": data
    })
    return ajax;
}

I get the following string (var info) , but I want an object:

[{"id_vehicle":"1","class":"business","brand":"Citroen","model":"C5 2.0hdi","year":"2010","km":"129000"}]

Which when attempting to parse, produces the following error, as reported by the chrome console:

Uncaught SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (controller.php:127)
    at fire (jquery-3.1.1.js:3305)
    at Object.fireWith [as resolveWith] (jquery-3.1.1.js:3435)
    at done (jquery-3.1.1.js:9242)
    at XMLHttpRequest.<anonymous> (jquery-3.1.1.js:9484)

How can I return the JSON object from PHP through ajax?

    
asked by javialm 07.04.2017 в 00:45
source

3 answers

0

I have already solved it, it is nothing of the JSON parse, etc.

The problem was simply that the document was stored in UTF-8 encoding with BOM.

BOM is the character that was inserted in the response and therefore could not apply the JSON.parse ();

The solution is to save to UTF-8 without BOM.

    
answered by 07.05.2017 / 15:23
source
1

Based on your return code echo json_encode($this->rows); I tell you the following. You must go through the rows (it seems that this is the direct result of the statement in your database) so that you arm a valid JSON element for each row. Ahem, is just an example for you to analyze and apply to your case.

In PHP

$json="";

foreach($datas as $this->rows)
{
    $json.=($json==""?"":",")."{nombredelvalor1: '".$datas[Identificador1]."',"; // Identificador1 indice o nombre de columna; esto para texto
    $json.="nombredelvalor2: ".$datas[Identificador2].","; // Identificador2 indice o nombre de columna; esto para un numero o valor booleano (true o false)
    // lo demas necesario
    $json.="nombredelvalorN: ".$datas[IdentificadorN]."}";
 }

$json = "{datos: [".$json."]}";

echo json_encode($json); // finalmente lo retornas

In JavaScript where you receive

If on the client's side you need a conversion like

var recibido =$.parseJSON(data); //  donde "data" es la respuesta de la ejecución AJAX

Of course, the previous point you assign to a variable and what was formed in PHP you will access with recibido.datos .

    
answered by 07.04.2017 в 01:02
-1

Maybe it's the parser, try it with this:

var info = $.parseJSON(data);

instead of:

info = JSON.parse(info); //AQUI EL ERROR
    
answered by 07.04.2017 в 00:52