【问题标题】:Google Map Places autocomplete api throws an error when called through ajax通过 ajax 调用时,Google Map Places 自动完成 api 会引发错误
【发布时间】:2013-06-26 05:21:58
【问题描述】:

我试图通过将其连接到谷歌地图位置自动完成 api,在用户键入位置框时自动建议位置名称(使用 jquery 自动完成)。但是 ajax 响应给我一个错误。

但是当我直接在浏览器中复制并粘贴相同的 api url 时,它会返回结果。

我做错了什么?

API 网址:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=cola&sensor=false&key=[我的钥匙]

我的萤火虫截图

https://docs.google.com/file/d/0B7ZMqGCQDtvkcEhsUTVQWTJhWXc/edit?usp=sharing

jQuery 代码

jQuery("#my_location").autocomplete(
{
    source: function( request, response ) 
    {
        jQuery.ajax(
        {
            url: "https://maps.googleapis.com/maps/api/place/autocomplete/json",

            type: "GET",


            data: {
                        input: request.term, //jQuery("#my_location").val(),
                        sensor: "false",
                        key: [my key]
            },

            success: function( data ) 
            {
                response( $.map( data.predictions, function( item ) 
                {
                    return {
                        label: item.description, //item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                        value: item.description
                    }
                }));

                //console.log(data);

            },

            error: function(MLHttpRequest, textStatus, errorThrown) 
            {
                console.log("Error: " + errorThrown)
            }

        });
    },

    minLength: 2

}); // autocomplete

【问题讨论】:

    标签: google-maps location jquery-autocomplete


    【解决方案1】:

    我认为这是因为 CORS。原谅一个非解决方案的答案,但也许它作为指导有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 2017-10-02
      相关资源
      最近更新 更多