从ajax查询返回的数组看起来不错,但其器官未正确显示 - php

我正在将地图视口边界的坐标传递给服务器,该服务器以jsoned数组的形式返回此区域中db中的所有坐标。
当我不使用ajax并发送硬编码数字而不是参数时,它工作正常:

<script type="text/javascript">
var coordinatesMap = 
<?php
    global $wpdb;
    $sql = "SELECT user_id, lat, lng FROM coordinates WHERE lat>30 and lat<50 and lng>-80 and lng<20";
    $rows = $wpdb->get_results($sql, OBJECT_K);

    if (is_array($rows)) {
        echo json_encode($rows);
    } else {
        echo '{}';
    }
?>;

alert(coordinatesMap);

for (var id in coordinatesMap)
{
    if (coordinatesMap.hasOwnProperty(id))
    {
        alert(id);
        alert(coordinatesMap[id].lat);
        alert(coordinatesMap[id].lng);
    }
}                               
</script>

唯一奇怪的是,alert(coordinatesMap);发出此警报[object Object]。但这不是问题。
其余警报都可以:177、40.058,-74.405、178、40.714,-74.005。

但是在“现实生活”中,我必须将参数传递给服务器,因此我使用如下的ajax:
Ajax调用:

$.ajax({
    type: "POST",
    url: "markers.php",
    data: ({'southWestLat' : southWestLat , 'southWestLng' : southWestLng , 'northEastLat' : northEastLat , 'northEastLng' : northEastLng}),
    success: function(coordinatesMap){
        alert( coordinatesMap );
        for (var id in coordinatesMap)
        {
            if (coordinatesMap.hasOwnProperty(id))
            {
                alert(id);
                alert(coordinatesMap[id].lat);
                alert(coordinatesMap[id].lng);
            }
        }                               
    }
});

PHP脚本:

<?php

    global $wpdb;
    $sql = "SELECT user_id, lat, lng FROM coordinates WHERE lat>".$_POST["southWestLat"]." and lat<".$_POST["northEastLat"]." and lng>".$_POST["southWestLng"]." and lng<".$_POST["northEastLng"];
    $rows = $wpdb->get_results($sql, OBJECT_K);

    if (is_array($rows)) {
        echo json_encode($rows);
    } else {
        echo "failure";
    }
?>

这次alert(coordinatesMap);提供了一个不错的数组-{"177":{"user_id":"177","lat":"40.0583238","lng":"-74.4056612"},"178":{"user_id":"178","lat":"40.7143528","lng":"-74.0059731"}}
但是以下警报是我真正感兴趣的警报,它们给出了无效的值并无限循环:`0,未定义,未定义,1,未定义,未定义,2等...
你能说发生什么吗?

参考方案

尝试将“ dataType:json”添加到您的ajax请求中,例如

$.ajax({
    type: "POST",
    url: "markers.php",
    dataType: "json",
    data: ({'southWestLat' : southWestLat , 'southWestLng' : southWestLng , 'northEastLat' : northEastLat , 'northEastLng' : northEastLng}),
    success: function(coordinatesMap){
        alert( coordinatesMap );
        for (var id in coordinatesMap)
        {
            if (coordinatesMap.hasOwnProperty(id))
            {
                alert(id);
                alert(coordinatesMap[id].lat);
                alert(coordinatesMap[id].lng);
            }
        }                               
    }
});

PHP:将数据从二维数组复制到一维数组的最快方法 - php

我有一个巨大的二维PHP数组,带有500万行。$t = [ [ "id" => 1, "name" => "foo" ], [ "id" => 2, "name" => "bar" ] ]; 现在,我必须将此数组的I…

PHP-MySQL结果转换为JSON - php

我试图了解如何将MySQL结果转换为JSON格式,以便以后可以在Javascript中使用此JSON来构建HTML表。但是我的代码只是产生大量的空值,我还不明白为什么。$result = mysqli_query($con, "SELECT * FROM Customers"); $test = json_encode($result);…

PHP Count数组元素 - php

嗨,有人可以解释为什么这会返回“数组由0个元素组成”。 :$arr = array(1,3,5); $count = count($arr); if ($count = 0) { echo "An array is empty."; } else { echo "An array has $count elements.…

PHP:从函数返回值并直接回显它? - php

这可能是一个愚蠢的问题,但是……的PHPfunction get_info() { $something = "test"; return $something; } html<div class="test"><?php echo get_info(); ?></div> 有没有办…

PHP str_replace是否具有大于13个字符的限制? - php

直到击中第13个字符为止。一旦str_ireplace在cyper数组中击中“ a”,str_ireplace就停止工作。数组的大小有限制吗?请记住,如果键入“ abgf”,我会得到“ nots”,但是如果键入“ abgrf”,我应该得到“ notes”,那么我就会得到“ notrs”。机架使我的大脑无法解决。$_cypher = array("n…