【问题标题】:CORS error in Google Maps Geocoding APIGoogle Maps Geocoding API 中的 CORS 错误
【发布时间】:2015-07-23 20:49:32
【问题描述】:

我遇到了一个相当突然的 Google 地图地理编码问题。直到昨天一切正常,但从那时起,我在向他们的 Geocoding API 发出的任何请求时都收到了 CORS 错误。

我的要求:

https://maps.googleapis.com/maps/api/geocode/json?latlng=18.92187618976372,72.82581567764282&key=<API_KEY>

我的请求标头:

Provisional headers are shown
Accept:application/json, text/plain, */*
Origin:http://domain
Referer:http://domain/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36
X-FirePHP-Version:0.0.6

响应标头:

access-control-allow-origin:*
alternate-protocol:443:quic,p=1
cache-control:public, max-age=86400
content-encoding:gzip
content-length:1279
content-type:application/json; charset=UTF-8
date:Thu, 23 Jul 2015 09:50:49 GMT
expires:Fri, 24 Jul 2015 09:50:49 GMT
server:mafe
status:200
vary:Accept-Language
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block

错误信息:

XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/geocode/json?latlng=18.92187618976372,72.82581567764282&key=<API_KEY>. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://domain' is therefore not allowed access.

【问题讨论】:

  • 你的代码是什么样的?
  • @geocodezip,我解决了。看看下面。 :)

标签: javascript google-maps


【解决方案1】:

我的系统是这样设置的,即我的前端是从我的逻辑服务器的跨域提供服务的。为此,我将withCredentials 标志设置为true。由于 Maps 的 API 使用 * 通配符,withCredentials 给出了错误。

为了解决这个问题,我为 Maps 调用使用了单独的 XHR 方法,其中标志设置为 false。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 2013-06-16
    • 2013-12-27
    • 2021-03-01
    • 2020-09-07
    • 2016-08-02
    • 2016-09-18
    • 2017-08-17
    相关资源
    最近更新 更多