简体   繁体   English

无法通过Google Map API的getjson获取json值

[英]Can't get the json value by getjson from google map api

Can't get the json value by getjson from google map api 无法通过Google Map API的getjson获取json值

HTML 的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 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
            });
          });
        });

        }

Inside $.getjson or $.ajax not executing anything. 在$ .getjson或$ .ajax内部不执行任何操作。

Another approach that is not working 另一种方法不起作用

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

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

Two reasons I could identify for you not getting a response from Places API - 我可以为您确定没有收到Places API响应的两个原因-

1) You are using data type JSONP instead of JSON while the API will return results in JSON format. 1)您使用的是数据类型JSONP而不是JSON,而API将以JSON格式返回结果

2) Even though you wish to use JSONP and not JSON, you did not implement a callback in order to parse results into JSONP. 2)即使您希望使用JSONP而不是JSON,您也没有实现用于将结果解析为JSONP的callback

For normal use case, I would recommend to use JSON over JSONP . 在正常使用情况下,我会建议使用JSONJSONP

Check these resources for difference between the two: 检查以下资源以了解两者之间的区别:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM