I have a problem, no matter how hard I look for it, I can not find how I can access the array that I obtained after having made a query using AJX with PHP and sent the result in JSON format to javascript. I hope you can help me, I do not understand how many tutorials I've seen. My code is as follows:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<section>
<div class="form">
<input type="text" name="caja_busqueda" id="caja_busqueda">
</div>
<div id="datos">
</div>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="main.js"></script>
</body>
</html>
function buscar_datos(consulta)
{
$.ajax({
url: 'buscar.php',
type: 'POST',
data: {consulta: consulta}
})
.done(function (respuesta) {
//Aquí no encuentro la forma de recuperar la matriz de datos que envíe desde PHP en formato JSON.
});
}
$(document).on('keydown', '#caja_busqueda', function () {
var valor = $(this).val();
buscar_datos(valor);
});
My PHP
<?php
$buscar = str_replace(" ", "%", $_POST['consulta']);
if ($buscar != "") {
$mysqli = new mysqli("servidor", "usuario", "contraseña", "bd_secjo");
mysqli_set_charset($mysqli, 'utf8');
$query = "SELECT Id_Contacto, Nombres, Apellido_Paterno, Apellido_Materno FROM bd_secjo.contactos where concat(nombres, ' ', apellido_paterno, ' ', apellido_materno) like '%" . $buscar . "%' limit 10;";
$resultado = $mysqli->query($query);
$ArrayDatos = ObtenerArraySQL($resultado);
echo json_encode($ArrayDatos);
$mysqli->close();
}
function ObtenerArraySQL($SQL) {
$rawdata = array();
$i = 0;
while ($row = mysqli_fetch_array($SQL)) {
$rawdata[$i] = $row;
$i++;
}
}