题意:Google Maps 地理编码 API 中的 CORS 错误。
问题背景:
I'm facing a rather sudden Google Maps Geocoding issue. It was all working fine till yesterday but since then I've been getting a CORS error on any request made to their Geocoding API.
我遇到了一个突发的 Google Maps 地理编码问题。直到昨天一切都正常,但从那时起,我在对他们的地理编码 API 发出的任何请求中都收到了 CORS 错误。
My request:
请求头
https://maps.googleapis.com/maps/api/geocode/json?latlng=18.92187618976372,72.82581567764282&key=<API_KEY>
My request headers:
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
Response headers:
响应头
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
Error message:
错误信息
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.
问题解决:
My system was setup in such a way that I have my front end is being served from a cross domain from my logic server. For this, I had set my withCredentials
flag as true
. Since Maps' API uses a *
wildcard, withCredentials
was giving an error.
我的系统设置是前端从与我的逻辑服务器跨域的地方提供服务。为此,我将 `withCredentials` 标志设置为 `true`。由于 Maps 的 API 使用了 `*` 通配符,`withCredentials` 就出现了错误。
To solve this, I used a separate XHR method for Maps calls where the flag was set as false.
为了解决这个问题,我对 Maps 调用使用了一个单独的 XHR 方法,其中该标志设置为 `false`。