It tells me that the value I'm calling is undefined in json

0

It tells me that it's undefined the name of the variable that I'm asking Jquery, but I put console.log (data) and it returns the complete array and with its variables

$(function () {
array_producto = [];
$.ajax({
    url: "ofertas_guardadas",
    dataType: 'json',
    contentType: "application/json",
    error: function () {
        alert("Error");
    },
    success: function (data) {
        console.log(data);
        $.each(data, function (i, item)
        {
            var json = item.nombre;
            alert(json);
        });
    }
});

servlet

 @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/plain");
    HttpSession sesion = request.getSession(false);
    PrintWriter out = response.getWriter();
    int id_consumidor = Integer.parseInt(sesion.getAttribute("id_consumidor").toString());
    String producto;
    String nombre, precio_oferta, precio_normal, id, ruta;
    JSONArray lista = new JSONArray();
    productoFacade.oferta_guardadas(id_consumidor);
    for (Producto p : productoFacade.oferta_guardadas(id_consumidor)) {
        nombre = p.getNombreProducto();
        id = p.getIdProducto().toString();
        precio_normal = p.getPrecioNormal().toString();
        precio_oferta = p.getPrecioOferta().toString();
        ruta = p.getRutaImagen();
        producto = new StringBuilder("{").
                append("\"nombre\":\"" + nombre + "\",").
                append("\"id\":\"" + id + "\",").
                append("\"precio_normal\":\"" + precio_normal + "\",").
                append("\"precio_oferta\":\"" + precio_oferta + "\",").
                append("\"ruta\":\"" + ruta + "\"").
                append("}").toString();
        lista.put(producto);
    }
    out.print(lista.toString());
}

I also tried this

        success: function (data) {
        console.log(data);
        $.each(data, function (i, item)
        {
            console.log(data[i].nombre)
            var json = JSON.parse(data[i].nombre);
            alert(json);
        });

This image shows what the console.log () prints

    
asked by Tomás Ignacio Torres Calderón 28.10.2017 в 02:45
source

2 answers

0

You have to convert the variable item to json to be able to access the properties using the method JSON.parse() :

$.each(data, function (i, item) {
     var json = JSON.parse(item).nombre;
     alert(json);
});

Although it looks like a json, actually every element of the array data is a string that contains a value in json format and as a string does not have a property called nombre , it shows you Undefined .

    
answered by 28.10.2017 в 03:48
0

Thanks to everyone for the advice, but I could not do it that way, so I looked for an alternative and everything went great. I leave the solution, in case someone serves. Greetings.

Servlet:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/plain");
    HttpSession sesion = request.getSession(false);
    PrintWriter out = response.getWriter();
    int id_consumidor = Integer.parseInt(sesion.getAttribute("id_consumidor").toString());
    Gson gson = new Gson();
    Producto producto = new Producto();
    List<Producto> lista = new ArrayList<>();
    productoFacade.oferta_guardadas(id_consumidor);
    for (Producto p : productoFacade.oferta_guardadas(id_consumidor)) {
        producto.setNombreProducto(p.getNombreProducto());
        producto.setIdProducto(p.getIdProducto());
        producto.setPrecioNormal(p.getPrecioNormal());
        producto.setPrecioOferta(p.getPrecioOferta());
        producto.setRutaImagen(p.getRutaImagen());
        lista.add(producto);
    }
    String json = gson.toJson(lista);
    out.print(json);
}

Javascript

$.ajax({
    url: "ofertas_guardadas",
    dataType: 'json',
    contentType: "application/gson",
    error: function () {

        alert("Error");
    },
    success: function (data) {
        $.each(data, function (i, item)
        {
            var json = item;
            console.log(json.idProducto);
            console.log(json.nombreProducto);
            console.log(json.precioNormal);
            console.log(json.precioOferta);
            console.log(json.rutaImagen);
            alert(json.nombreProducto);
        });

    }
});
    
answered by 28.10.2017 в 21:51