Eros Scoppiato Eros Scoppiato - 4 months ago 15
jQuery Question

Can't read json elements from mysql query via ajax

I need to add some geo-marker to my map.
the markers are in my mysql table on altervista.org
but my JavaScript says [object Object] every time i try...

here my php code:

require('connect.php');
$query = "SELECT latit, longit FROM segnalazioni";
$result = mysql_query($query);
$rows = array();
while ($row = mysql_fetch_assoc($result)){
$rows[] = $row;
}
echo json_encode($rows);


it returns:

[{"latit":"12.34","longit":"12.34"},{"latit":"56.78","longit":"56.78"},...]


here my JavaScript:

function addMarker(mapobj) {
$.getJSON( "http://####.altervista.org/map.php", function( data ) {
var items = [];
$.each( data, function( key1 , val1 ) {
items.push( "<li id='" + key1 + "'>" + val1 + "</li>" );
//next todo:
//mapobj.marker([latit, longit]).addTo(map).bindPopup("hi");
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
}


and on the end of my [body] i can see only:

[object Object]
[object Object]
[object Object]
...

Answer

Use this

$.each(result, function(key, value){
        $.each(value, function(key, value){
            console.log(key, value);
        });
    });