簡體   English   中英

無法通過Google Map API的getjson獲取json值

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

無法通過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/

我可以為您確定沒有收到Places API響應的兩個原因-

1)您使用的是數據類型JSONP而不是JSON,而API將以JSON格式返回結果

2)即使您希望使用JSONP而不是JSON,您也沒有實現用於將結果解析為JSONP的callback

在正常使用情況下,我會建議使用JSONJSONP

檢查以下資源以了解兩者之間的區別:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM