I can not display the data I get in JSON format, this is the code I use.
This is my jquery function, I take the data of a field, I send them by post and I receive the answer which I send by the function cargar_productos
that I want to show and I can not.
$('#buscar_datos').on('click', function(){
var datos_buscar = $('#codigo').serialize();
// alert(datos_buscar);
// return false;
$.ajax({
data: datos_buscar,
url: 'funciones/listar_productos.php',
type: 'post',
success: function(datos){
cargar_productos(datos);
}
});
return false;
});
Here is the function I want to use to show the JSON data but I do not understand how to show it. I try that way but there is no case. If I get a alert
with "data" that I get from my php, it shows me the JSON.
function cargar_productos(datos){
$('#Salida').html(datos[0].Nombre);
}
The JSON is next
{"datos":
[{"id":"19","Nombre":"1","Precio":"2","Categoria":"3","Codigo":"4"}]}
And the php is this
<?php
require_once 'conexion.php';
$codigo=$_POST["codigo_producto"];
$sql="SELECT * FROM productos WHERE codigo = $codigo";
$resultado = mysqli_query($mysqli,$sql);
if(!$resultado){
die("Error");
}else{
while($data = mysqli_fetch_assoc($resultado)){
$arreglo["datos"][] = array_map("utf8_encode", $data);
}
echo json_encode($arreglo);
}
mysqli_free_result($resultado);
mysqli_close($mysqli);
?>
Thanks in advance!