我需要在我的 map 上添加一些地理标记。 标记位于 altervista.org 上我的 mysql 表中 但每次我尝试时,我的 JavaScript 都会说 [object Object]...
这是我的 php 代码:
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);
它返回:
[{"latit":"12.34","longit":"12.34"},{"latit":"56.78","longit":"56.78"},...]
这是我的 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" );
});
}
在我的[ body ]的末端我只能看到:
[object Object]
[object Object]
[object Object]
...
最佳答案
根据jquery.each参数是
- indexInArray 、值和 NOT 键、值
所以代码是:
$(function () {
var data = [{"latit": "12.34", "longit": "12.34"}, {"latit": "56.78", "longit": "56.78"}];
var items = [];
$.each(data, function(indexInArray , value) {
items.push( "<li id='" + indexInArray + "'>latit: " + value.latit + ' longit:' + value.longit + ' OR '+ JSON.stringify(value) + "</li>" );
//next todo:
//mapobj.marker([latit, longit]).addTo(map).bindPopup("hi");
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
关于php - 无法通过ajax从mysql查询读取json元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38313661/