【问题标题】:Can't get the json value by getjson from google map api无法通过谷歌地图 api 的 getjson 获取 json 值
【发布时间】:2015-03-30 06:46:54
【问题描述】:

无法通过 google map api 的 getjson 获取 json 值

HTML

<html>
<head>
</head>
<body>
<input data-mini="true" data-theme="a" id="search" name="search" placeholder="Search for Restaurants" type="search">
<input data-inline="true" id="submit" onclick="getRestaurants()" type="submit" value="Go">
</body>
</html>

JS

    function getRestaurants()
        {
            var search=$("#search").val();
            var prestring="restaurant+in+";

            var jsonRList="https://maps.googleapis.com/maps/api/place/textsearch/json?query="+prestring+search+"&key=API_KEY";
            var xmlRList="https://maps.googleapis.com/maps/api/place/textsearch/xml?query="+prestring+search+"&key=API_KEY";
            alert(jsonRList);//working

            //NOT working
            $.getJSON(jsonRList, function (data) {
                alert(data.results[0].name);//returns nothing
                alert("hello2");//returns nothing
                });

            //working
            var data = '{"name": "abc","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';
            var json = JSON.parse(data);
            alert(json.phoneNumber[0].number);

            alert("hello3");//working

            //NOT working
            $.ajax({
                  url: 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=kolkata&key=API_KEY',
                  dataType: 'jsonp',
                  success: function(data){
                     alert(data);//returns nothing
                     alert("ajax");//returns nothing
                  }
                });

            alert("hello4");//working

//Not working
    $(document).ready(function(){
          $("submit").click(function(){

            $.getJSON( jsonRList, function( data ) {
            alert(data);//returns nothing
            alert("hello5");//returns nothing
            });
          });
        });

        }

在 $.getjson 或 $.ajax 内部没有执行任何操作。

另一种不起作用的方法

$("button").click(function() {
....

https://jsfiddle.net/sphakrrokr/r9xbctnr/

【问题讨论】:

标签: javascript html json google-maps


【解决方案1】:

我可以为您确定没有从 Places API 获得响应的两个原因 -

1) 您正在使用数据类型 JSONP 而不是 JSON,而 API 将以 JSON 格式 return results

2) 即使您希望使用 JSONP 而不是 JSON,您也没有实现 callback 来将结果解析为 JSONP。

对于正常使用情况,我建议使用JSON 而不是JSONP

检查这些资源以了解两者之间的区别:

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多