AJAX,JSON和PHP的问题返回值

时间:2013-08-02 05:58:33

标签: php ajax json

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

的index.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>

filterstores.php

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

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

echo json_encode($stores);

以下将是我在点击&#34;分享位置后得到的结果&#34;按钮从浏览器弹出

Latitude: sdf
Longitude: sdfsd

5 个答案:

答案 0 :(得分:2)

这一行:

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

未指定 currloc 作为参数?

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

答案 1 :(得分:1)

尝试搜索'sdfsdf'或“sdfsdf”和'sdf'或“asdf”

您将自己解决问题。

答案 2 :(得分: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);

答案 3 :(得分: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);

答案 4 :(得分: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);

希望它会有所帮助