在预检时,对Google地图添加地点的POST调用失败

时间:2016-12-11 21:37:52

标签: google-places-api

我正在尝试向Google地图添加自定义地点,为此我尝试使用此代码发送POST请求:

httpPostAsync('https://maps.googleapis.com/maps/api/place/add/json?key=<MY_KEY>', 
      {
        location: {
          "lat": 32.12345,
          "lng": 32.12345
        },
        "accuracy": 50,
        "name": "Test123"
      },
      function(response) {
        console.log(response);
    });   


 function httpPostAsync(theUrl, params, callback)
  {
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.onreadystatechange = function() { 
          if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
              callback(xmlHttp.responseText);
      }
      xmlHttp.open("POST", theUrl, true); // true for asynchronous 
      //Send the proper header information along with the request
      xmlHttp.setRequestHeader("Content-type", "application/json");
      xmlHttp.setRequestHeader("Host", "maps.googleapis.com");

      xmlHttp.send(JSON.stringify(params));
  }

我从我的网站上运行它,让我们说地址是https://www.example.com/maps.html。 我有一个API密钥,其密钥限制来自www.example.com/*。 每当我运行此代码时,我都会收到此错误:

XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/add/json?key=<MY_KEY>. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.example.com' is therefore not allowed access. The response had HTTP status code 400.

任何想法可能是什么问题?

0 个答案:

没有答案