使用 google place api 添加一个地点,使用 jQuery.ajax 进行定位

Adding a place with google places api using jQuery.ajax

本文关键字:使用 jQuery ajax 定位 api place google 添加 一个      更新时间:2023-09-26

当我尝试向 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"}
                    }
                });
});
我也

一直在努力让它工作。执行代码后,我在 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对象中指定了它。

幸运的是,有一个客户端JavaScript库。不幸的是,它不支持:(添加地点或事件。

我希望谷歌将来为地方API添加更多支持。