0

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

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

        }

Inside $.getjson or $.ajax not executing anything.

Another approach that is not working

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

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

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • You should use offical Google Place API : http://stackoverflow.com/questions/11642418/trying-to-use-google-places-api-with-jquerys-getjson-function – jmgross Mar 30 '15 at 10:27

1 Answers1

0

Two reasons I could identify for you not getting a response from Places API -

1) You are using data type JSONP instead of JSON while the API will return results in JSON format.

2) Even though you wish to use JSONP and not JSON, you did not implement a callback in order to parse results into JSONP.

For normal use case, I would recommend to use JSON over JSONP.

Check these resources for difference between the two:

Community
  • 1
  • 1
Kay_N
  • 987
  • 5
  • 12