无法通过google map api中的getjson获取json值 [英] Can't get the json value by getjson from google map api

查看:123
本文介绍了无法通过google map api中的getjson获取json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

HTML

 < HTML> 
< head>
< / head>
< body>
< input data-mini =truedata-theme =aid =searchname =searchplaceholder =Search for Restaurantstype =search>
< input data-inline =trueid =submitonclick =getRestaurants()type =submitvalue =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); //工作

//不工作
$ .getJSON(jsonRList,function(data){
alert(data.results [0 ] .name); //不返回
alert(hello2); //不返回任何内容
});

//工作
var data ='{name:abc,age:30,address:{streetAddress:88 8nd Street,城市:纽约},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); //工作

//不工作
$ .ajax({
url:'https:// maps.googleapis.com/maps/api/place/textsearch/json?query=kolkata&key=API_KEY',
dataType:'jsonp',
成功:函数(数据){
alert(data); // return nothing
alert(ajax); // returns nothing
}
});

alert(hello4); //工作

//不工作
$(document).ready(function(){
$ (submit)。click(function(){

$ .getJSON(jsonRList,function(data){
alert(data); //不返回任何内容
alert( hello5); // return nothing
});
});
});


在$ .getjson或$ .ajax中不执行任何操作。

另一种行不通的方法

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

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

解决方案

您可以通过Places API找到答案的两个原因 -


$ b <1>您正在使用数据类型JSONP,而不是JSON,而API将以JSON格式返回结果。 2)即使你想使用JSONP而不是JSON,你也没有实现回调以便将结果解析为JSONP。

对于正常使用情况,我建议使用 JSON 来覆盖 JSONP

检查这些资源是否存在差异:


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/

解决方案

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:

这篇关于无法通过google map api中的getjson获取json值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆