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

Javascript 无法通过从google map api获取getjson获取json值,javascript,html,json,google-maps,Javascript,Html,Json,Google Maps,无法通过从google map api获取getjson获取json值 HTML 在$.getjson或$.ajax内部,不执行任何操作 另一种不起作用的方法 $("button").click(function() { .... 我可以找出两个原因,让您无法从Places API获得响应- 1您使用的是数据类型JSONP而不是JSON,而API将使用JSON格式 2即使您希望使用JSONP而不是JSON,您也没有实现回调来将结果解析为JSONP 对于正常用例,我建议使用JSON而不是JSON

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

HTML

在$.getjson或$.ajax内部,不执行任何操作

另一种不起作用的方法

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

我可以找出两个原因,让您无法从Places API获得响应-

1您使用的是数据类型JSONP而不是JSON,而API将使用JSON格式

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

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

检查这些资源以了解两者之间的差异:


您应该使用官方Google Place API:
    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
            });
          });
        });

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