Hi I have the following table that I am calling using a controller
Controller
$respuesta = GestorOITModels::obtenerViasModel("transporte"); //nombre de la tabla
$datos = array($respuesta);
echo json_encode($datos);
JS file, where I try to save the table
$(".content-wrapper").ready(function(){
var vias;
$.ajax({
url: "views/ajax/OIT.php",
method: "GET",
dataType: "json",
success: function(respuesta) {
if (respuesta == 0) {
console.log("malo");
} else {
vias=respuesta[0];
console.log("Primer console", vias);
vias.forEach( function(valor, indice, array) {
console.log("En el índice " + indice + " hay este valor: " + valor);});
}
}
});
})
But when I use the browser console it looks like this:
Data returned using JSON.stringify(response)
[[{"0":"AEREO","tipo":"AEREO"},{"0":"DIRECTO","tipo":"DIRECTO"},{"0":"MARITIMO","tipo":"MARITIMO"},{"0":"OTROS","tipo":"OTROS"},{"0":"TERRESTRE","tipo":"TERRESTRE"}]]
What should I correct so that when I iterate it again I am left with, for example:
"Via[0]="AEREO"-Via[1]="DIRECTO"
, etc.
Thank you very much for the help.
The problem you are having is that each element of the array is an object that has two properties with the same value, so if you try to display its content it will tell you that it is of type
Object
.To show its content you must indicate the property you want to show, I add several examples of how to access each of them: