Display data from my JSON

1

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!

    
asked by Niel Jobs 15.05.2018 в 22:18
source

4 answers

1

Reviewing your concern and making the local example you can access the elements as follows:

var jsonTest = {"datos": 
[{"id":"19","Nombre":"1","Precio":"2","Categoria":"3","Codigo":"4"}]};

console.log(jsonTest["datos"][0]["id"]);

Therefore taking into account the previous example in your function js would be of the sig. way:

function cargar_productos(datos){
  $('#Salida').html(datos["datos"][0]["Nombre"]);
}

Now the problem was in the hierarchy and therefore when reviewing it would be as I enunciate in my example. Greetings. I hope you find it useful.

    
answered by 15.05.2018 / 22:33
source
2

It's simple, the error is in how you are calling the variable and the json, then you superimpose the names and omit a step. The function cargar_productos() should be like this:

function cargar_productos(datos) {
  $('#Salida').html(datos.datos[0].Nombre);
}

I would re-name it to avoid confusion next time:

function cargar_productos(data) {
  $('#Salida').html(data.datos[0].Nombre);
}
    
answered by 15.05.2018 в 22:31
1

Receive them in a variable like this:

var resultados = datos.d;

Then you just sent it to your show.

cargar_productos(resultados);

and manipulate them as you did before

resultados[0].Nombre
    
answered by 15.05.2018 в 22:36
1

It can be done, in javascript with a for..of.. :

function cargar_productos(datos){
    for (var dato of datos){
        $("#Salida").html(dato.Nombre);
    }
}

If you are only going to retrieve a data, this method would work, but in case the json has other direct daughter properties apart from "data", you should implement something like that (concept):

var cadena = "";
function cargar_productos(datos){
    for (var dato of datos){
       cadena+= dato.Nombre;
    }
}
 $("#Salida").html(cadena);

I hope it serves you: D

    
answered by 18.05.2018 в 14:59