即使结果有效,jQuery getJSON在查询Google地图时也会返回null

时间:2010-08-02 07:07:05

标签: jquery json

我似乎无法使这个简单的代码工作:

<script type="text/javascript">
$(document).ready(function(){ 
    $.getJSON('http://maps.google.com/maps/api/geocode/json?address=cebu&sensor=false&region=ph', 
    function(results) {
        alert(results);
    });
});
</script>

它总是警告null,即使json的结果是这样的:

{
  "status": "OK",
  "results": [ {
    "types": [ "locality", "political" ],
    "formatted_address": "Cebu City, Philippines",
    "address_components": [ {
      "long_name": "Cebu City",
      "short_name": "Cebu City",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Cebu",
      "short_name": "Cebu",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Central Visayas",
      "short_name": "Central Visayas",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "Philippines",
      "short_name": "PH",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 10.3455617,
        "lng": 123.8969328
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 10.3016523,
          "lng": 123.8329031
        },
        "northeast": {
          "lat": 10.3894650,
          "lng": 123.9609625
        }
      },
      "bounds": {
        "southwest": {
          "lat": 10.2594350,
          "lng": 123.8668156
        },
        "northeast": {
          "lat": 10.3991826,
          "lng": 123.9401150
        }
      }
    }
  } ]
}

我还需要做什么?我已经阅读了jquery手册,但我似乎无法使这个简单的例子工作。

谢谢!

1 个答案:

答案 0 :(得分:1)

http://docs.jquery.com/Getjson 第3段: “请注意,您尝试调用的站点需要支持JSON-P输出。”

您确定maps.google.com支持此功能吗?