使用jQuery.ajax添加google放置api的地方

时间:2012-06-23 11:14:32

标签: javascript jquery html ajax google-places-api

当我尝试向Google商家信息中添加地点时,我收到了错误消息:

{"readyState":0,"responseText":"","status":0,"statusText":"error"}

继承我的代码:

HTML:

<form id="addShopForm">

          ...

        <a type="submit" id="submitshop">Submit</a>

</form>

Jquery的:

 $('#submitshop').click(function(){
 $.ajax({
                    type: "POST",
                    url: "https://maps.googleapis.com/maps/api/place/add/json?sensor=true&key=asdEFasdBaasdasdasdMrN2ST5tRS5c",
                    crossDomain: true,
                    data: {
                       "location": {
                        "lat": 68.7425471,
                        "lng": 9.1370879
                       },
                       "accuracy": 50,
                       "name": "Test Shop!",
                       "language": "de"
                    },
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(JSON.stringify(msg));
                    },
                    error: function (errormessage) {
                        alert(JSON.stringify(errormessage));
                        //{"readyState":0,"responseText":"","status":0,"statusText":"error"}
                    }
                });
});

1 个答案:

答案 0 :(得分:0)

我一直在努力让这个工作起来。执行代码后,我在chrome调试器中发现了这个错误:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
https://maps.googleapis.com/maps/api/place/add/json?sensor=false&key={Key}
Failed to load resource: Origin http://localhost:8888 is not allowed by Access-Control Allow-Origin. 
https://maps.googleapis.com/maps/api/place/add/json?sensor=false&{key}
XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/add/json?sensor=false&key={Key}. 
Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin. 

所以似乎谷歌地图放置API不支持跨域,即使你已经在ajax对象中指定了它。

幸运的是,有一个client-side javascript library。不幸的是,它不支持添加地点或事件:(。

我希望谷歌将来能够为Places API添加更多支持。