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 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决