0

我正在从 https://developers.google.com/places/documentation/search处理 Google Places

添加 api 密钥后,此 url

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=

在浏览器中给了我很好的 json 响应。但是每当我想用 JQuery.Ajax() 解析这些响应时,它都会给我错误。

我找不到这个错误背后的原因。我该如何解决这些问题?谢谢。

JS:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&libraries=places"></script>


 <script type="text/javascript">

    $(document).ready(function() {
        $("#submit").click(function(event){
            var url="https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=<myKey>";

            $.ajax({
                url: url,
                type:'POST',
                dataType: 'json',
                success: function(data)
                {
                    alert("success");

                },
                error:function(){
                    alert("error");
                }
            });

        });
    });

</script>

html:

<button id="submit">press</button>
4

2 回答 2

0

无法通过 AJAX 访问链接的资源,因为访问受到同源策略的限制,并且不支持 JSONP。

使用 javascript-places-library 的nearSearch在客户端获取结果。

于 2013-07-31T21:02:50.893 回答
-2

数据类型:'jsonp',

这对我有用!

于 2014-02-05T07:38:38.243 回答