2

我正在尝试编写一个脚本,将地理位置的 lat 和 lng 作为数组传递给 PHP,并使用 jQuery AJAX 向客户端返回一个值。我能找到的最好的解决方案是 JSON。我的以下脚本NULL出于某种我无法分辨的原因返回了我的值。

索引.html

<div id="geoloc"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
            if(navigator.geolocation){
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {
                $('#geoloc').html("<p>Geolocation is not supported by this browser</p>");
            }

            function showPosition(position) {
                var latlng = [ {"lat":position.coords.latitude, "lng":position.coords.longitude}];

                $.ajax({
                    type: "POST",
                    url: "libs/filterstores.php",
                    data: { json: JSON.stringify(latlng) },
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data){
                        $('#geoloc').html("Latitude: " + data.lat + "<br />Longitude: " + data.lng);
                    }
                });
            }

        });
</script>

过滤器存储.php

$currloc = json_decode($_POST['latlng'], true);

$stores = array('lat'=>$currloc['lat'],'lng'=>$currloc['lng']);

echo json_encode($stores);

以下将是我点击浏览器弹出的“共享位置”按钮后返回的结果

Latitude: sdf
Longitude: sdfsd
4

5 回答 5

2

这行应该:

    data: { json: JSON.stringify(latlng) },

不指定currloc作为参数?

IE

    data: { currloc: JSON.stringify(latlng) },
于 2013-08-02T06:16:30.267 回答
1

我认为您正在 AJAX 函数中进行双重 json 编码。如果编码时出现问题,JSON 函数会给你一个 null 作为答案。你只需要传递没有 json 编码的字符串,它肯定会工作:

function showPosition(position) {
$.ajax({
    type: "POST",
    url: "libs/filterstores.php",
    data: { lat:position.coords.latitude, lng:position.coords.longitude },        
    dataType: "json",
    success: function(data){
    $('#geoloc').html("Latitude: " + data.lat + "<br />Longitude: " + data.lng);
    }
});
}

您的 PHP 只需接受 POST,无需 json 编码:

PHP 文件:

$lat = $_POST['lat'];
$lng = $_POST['lng'];

$stores = array('lat'=>$lat,'lng'=>$lng);

echo json_encode($stores);
于 2013-08-02T06:34:16.957 回答
1

只需尝试搜索“sdfsd”或“sdfsd”和“sdf”或“sdf”

您将自己解决您的问题。

于 2013-08-02T06:19:01.717 回答
0

这是您可以发送和接收 JSON 数据的方式

$.ajax({
        type: "GET",
        url: "filterstores.php",
        data: {json: latlng},
        contentType: "application/json; charset=utf-8;",
        dataType: "json",
        success: function(data){
            $('#geoloc').html("Latitude: " + data.lat + "<br />Longitude: " + data.lng);
        }
});

和 PHP 文件

$currloc = $_GET['json'];
$stores = array('lat'=>$currloc[0]['lat'],'lng'=>$currloc[0]['lng']);
echo json_encode($stores);
于 2013-08-02T06:21:59.167 回答
0

尝试这个

$currloc = json_decode($_POST['json'], true);

反而

$currloc = json_decode($_POST['latlng'], true);

或者在 SCRIPT 中试试这个

     function showPosition(position) {
            $.ajax({
                type: "POST",
                url: "libs/filterstores.php",
                data: {"lat":position.coords.latitude, "lng":position.coords.longitude},
               dataType: "json",
                success: function(data){
                    $('#geoloc').html("Latitude: " + data.lat + "<br />Longitude: " + data.lng);
                }
            });
        }

在 PHP 文件中

$stores = array('lat'=>$_POST['lat'],'lng'=>$_POST['lng']);

echo json_encode($stores);

希望它会有所帮助

于 2013-08-02T06:34:03.797 回答