problems reading ajax array

2

What am I doing wrong?

I am making a ajax request that returns me as callback this type of array or object

[{"nombre":"Kabul"},{"nombre":"Qandahar"},{"nombre":"Herat"},{"nombre":"Mazar-e-Sharif"},{"nombre":"Otra"}]

At the time of reading this array I am doing it in the following way

$.ajax({

   type:"POST",
       url:url2,
       data:{code:code},
       datatype:"json",
       contenType: "application/json",
   }).done(function(response){

       var text=response;
       var obj=JSON.parse(text);

for (var propiedad in obj) {
  if (obj.hasOwnProperty(propiedad)) {
    console.log("En la propiedad '" + propiedad + "' hay este valor: " + 
    obj[propiedad]);
  }
}

the ouput is the next ...

En la propiedad '0' hay este valor: [object Object]
En la propiedad '1' hay este valor: [object Object]
En la propiedad '2' hay este valor: [object Object]
En la propiedad '3' hay este valor: [object Object]
En la propiedad '4' hay este valor: [object Object]

I'm waiting for you to throw me ...

En la propiedad '0' hay este valor: Kabul
En la propiedad '1' hay este valor: Qandahar
En la propiedad '2' hay este valor: Herat
etc...

the php file where this array comes from is this

$data=array();

     while($row= $stmt->fetch()){ 

        $data[] = array('nombre'=>$CiudadNombre );
               } 

     echo json_encode($data);

What am I doing wrong I do not understand some friendly hand plis?

    
asked by andy gibbs 01.11.2018 в 01:41
source

2 answers

4

It's an arrangement of objects, not an array of strings, try trying this

$.ajax({

   type:"POST",
       url:url2,
       data:{code:code},
       datatype:"json",
       contenType: "application/json",
   }).done(function(response){

       var text=response;
       var obj=JSON.parse(text);


       obj.forEach( ciudad => console.log(ciudad) );



       //Suponiendo que el nombre de la ciudad este en una propiedad llamada nombre
       obj.forEach( ciudad => console.log(ciudad.nombre) );

}
    
answered by 01.11.2018 / 02:04
source
1

You are returning an array of jsons, when you iterate over the json array, each element is a json.

In for in javascript iterates over the keys , that is, each you use the for (var propiedad in obj) {... , propiedad will be worth the key , which is not implicit, take the number, ie 0,1,2,3 ... etc

Then as propiedad = 0 , when doing obj[propiedad] , that is to say the index 0 of propiedad , the resulting value is {"nombre":"Kabul"} . This means that it is indeed an object.

To access that data, you simply have to access that property so obj[propiedad].name

for (var propiedad in obj) {
  if (obj.hasOwnProperty(propiedad)) {
    console.log("En la propiedad '" + propiedad + "' hay este valor: " + 
    obj[propiedad].name);
  }
}

In this option it would be worth checking if from your php it is necessary to return it as a json array instead of an array of only the names, which you could do

$ data = array ();

 while($row= $stmt->fetch()){ 

     $data[] = $CiudadNombre;
 } 
 echo json_encode($data);

and then you could show that info with the for of javascript as you have it now.

If you want to have a json array and iterate over each one, you would have to put another for in your json for now iterating over the json object as such

    
answered by 01.11.2018 в 02:01