Well I'm working with AJAX , JavaScript and of course jQuery , I've managed to bring the data response from the database so access and show the main data of this, the problem is that I have not been able to access the child elements that each parent has, I do not know how to do it, I have already tried several things and since yesterday I have been trapped, I will leave you my code so they can understand what I have done.
As you can see in the code I get the names of the parent categories, I display them with jQuery and inserting them in the HTML and everything looks good, the thing is and the sub-categories ?
Here JSON
[
{
"id": "28",
"name": "Restaurante",
"activo": "true",
"sub": [
{
"id": "29",
"name": "Comida Casera"
},
{
"id": "38",
"name": "Cafetería"
}
]
},
{
"id": "30",
"name": "Música",
"activo": "true",
"sub": [
{
"id": "31",
"name": "Academia de Música"
}
]
},
{
"id": "33",
"name": "Bienstar sexual ",
"activo": "true",
"sub": [
{
"id": "34",
"name": "Gel íntimo "
},
{
"id": "66",
"name": "Juguetes para adultos"
}
]
},
{
"id": "41",
"name": "Moda",
"activo": "true",
"sub": [
{
"id": "40",
"name": "Mochilas"
},
{
"id": "67",
"name": "Zapatos Hombre"
}
]
},
{
"id": "59",
"name": "Auto",
"activo": "true",
"sub": [
{
"id": "60",
"name": "Accesorio para automóviles "
},
{
"id": "61",
"name": "Lavado para automóviles "
}
]
},
{
"id": "63",
"name": "Belleza",
"activo": "true",
"sub": [
{
"id": "64",
"name": "Salón de Belleza"
},
{
"id": "65",
"name": "Productos de Belleza"
}
]
},
{
"id": "68",
"name": "Deporte",
"activo": "true",
"sub": [
{
"id": "69",
"name": "Gimnasios"
}
]
}
]
Here the JavaScript.
$.ajax({
type: 'GET',
url: 'http://www.kupomcity.com/gamma/api_v2.php?
_opt=categorias&_act=view',
dataType: 'json',
data: null,
beforeSend: function() {
//alert('Fetching....');
},
success: function() {
//alert('Fetch Complete');
},
error: function() {
//alert('Error');
},
complete: function(data) {
var ul = $("<ul class='navbar-nav'>");
for (var j = 0; j < data.responseJSON.length ; j++) {
ul.append("<li class='dropdown'><a href='templates/interior-categoria.html' class='nav-link' data-toggle='dropdown'>" + data.responseJSON[j].name + "<b class='caret'></b></a><ul class='dropdown-menu'><li><a href='templates/interior-categoria.html'>" + data.responseJSON[j].name + "</a></li></ul></li>");
}
$("#categorias").append(ul);
if (JSON.stringify(data.statusText) == '"OK"') {
//$('#cupones_usados').text(JSON.stringify(data.responseJSON));
//alert("cupones: " +JSON.stringify(data.responseJSON));
}
}
});