使用$ .ajax()自定义Google地方搜索返回错误

时间:2013-07-31 19:23:46

标签: google-maps jquery google-api google-places-api

我正在使用https://developers.google.com/places/documentation/search

处理Google商家信息

添加api key后这个网址

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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:-2)

dataType:'jsonp',

这对我有用!!