weixin_33744141 2013-02-01 19:56 采纳率: 0%
浏览 49

解析JSON Ajax响应

I'm running into trouble parsing a JSON response. I've done this lots of times but this is not working for a reason I cannot discern. Here is what I'm doing:

Before anyone suggests it - due to some requirements of the project I am working on I have to use the server side Google Geocoding web service, I can't do it all client side.

I'm using PHP for the server side portion. I'm using AJAX(via jQuery) to send the location to the server, getting the response and returning it encoded as JSON. Here is the server side code:

<?php
if(!empty($_POST['theLocation']))
{
    $loc = urlencode($_POST['theLocation']);

    $response = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$loc,+CA&sensor=false");
    echo  json_encode($response);
}
?>

Here is the client side call, where theLocation is a location name:

 var postdata = "theLocation=" + encodeURIComponent(theLocation);
$.ajax( {
    type : "POST",
    url : "GeoEncode.php",
    data :postdata ,
    dataType : "JSON",
    success : function(data) {
        data = $.parseJSON(data);

        lat = data.results[0].geometry.location.lat;
        lng = data.results[0].geometry.location.lng;

    },
    error: function (request, status, error) {
        log("Error: getLatLong Status - " + status + " ErrorText - " + error);
    }
});

When I try to access data.results[0] I get the error Uncaught TypeError: Cannot read property '0' of undefined. This obviously means that there is no results property. However if I do a console.log(data) This is what I get:

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "Halifax",
                    "short_name": "Halifax",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Halifax",
                    "short_name": "Halifax",
                    "types": [
                        "administrative_area_level_3",
                        "political"
                    ]
                },
                {
                    "long_name": "Halifax Regional Municipality",
                    "short_name": "Halifax Regional Municipality",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Nova Scotia",
                    "short_name": "NS",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "Canada",
                    "short_name": "CA",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "Halifax, NS, Canada",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 44.7035312,
                        "lng": -63.55963089999999
                    },
                    "southwest": {
                        "lat": 44.5949302,
                        "lng": -63.68627009999999
                    }
                },
                "location": {
                    "lat": 44.6488625,
                    "lng": -63.5753196
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 44.7035312,
                        "lng": -63.55963089999999
                    },
                    "southwest": {
                        "lat": 44.5949302,
                        "lng": -63.68627009999999
                    }
                }
            },
            "types": [
                "locality",
                "political"
            ]
        }
    ],
    "status": "OK"
}

This is valid JSON, there is clearly a results property. I don't know why I am having trouble with this, I must be missing something but through my debugging I have yet to find out what. If anyone could offer any suggestions it would be very much appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • weixin_33737134 2013-02-01 19:59
    关注

    You are settingdataType: 'json'. This means that jQuery will parse the result as JSON for you.

    Lose the data = $.parseJSON(data); line, then it should work.

    Also, as @SLaks pointed out: in your PHP, $response is already in JSON format. No need to json_encode it. Just echo $response.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog