I have the following code where I make a query.
include_once 'conexion.php';
$sql = "SELECT * FROM herramientas";
$resultado = mysqli_query($conn, $sql);
if (!$resultado) {
die("Error");
} else {
while ($data = mysqli_fetch_assoc($resultado)) {
$arreglo["data"][] = $data;
}
echo json_encode($arreglo);
}
mysqli_free_result($resultado);
mysqli_close($conexion);
I thought it was throwing me an error because of the accents but it's not like that. It throws me the following error DataTables warning: table id=dataTable - Invalid JSON response
. But if I perform to another table it works fine for me. Should it be because of the formats or content of the fields?
var table = $('#dataTable').DataTable({
"destroy": true,
"ajax": {
"method": "POST",
"url": "include/JSON.php"
},
"order": [
[0, "desc"]
],
"columns": [
{
"data": "Codigo"
}, {
"data": "Estado"
}, {
"data": "Descripcion"
}],
"language": idioma_espanol
});
I already addedJSON_UNESCAPED_UNICODE
Solution
$tabla = "";
if (!$resultado) {
die("Error");
} else {
while ($data = mysqli_fetch_assoc($resultado)) {
// $arreglo["data"][] = $data;
$tabla.='{"Codigo":"'.$data['Codigo'].'","Estado":"'.$data['Estado'].'"},';
}
$tabla= substr($tabla,0,strlen($tabla)-1);
echo '{"data":['.$tabla.']}';
//echo json_encode($arreglo,JSON_UNESCAPED_UNICODE);
}
mysqli_free_result($resultado);
mysqli_close($conexion);
I don't quite understand the problem you have, if you don't want the special characters to be converted to \u format....
The receiver has to reverse the encoding, normally it does it automatically, for example in php using json_decode(data_json) converts it without further ado.
But you still want them not to be converted, use the JSON_UNESCAPED_UNICODE option
Test
You can also make sure that you don't get any strange characters by doing the following:
here you have the documentation documentation