我在本地机器上测试一个简单的 mapbox 示例时收到以下消息(在端口 localhost:8000 上使用 python SimpleHTTPServer 提供服务):
10:06:53.909 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://a.tiles.mapbox.com/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/[...]. (Reason: CORS request failed).1 <unknown>
html文件很简单:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.17.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.17.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = <myaccesstoken>;
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v8', //stylesheet location
center: [-74.50, 40], // starting position
zoom: 9 // starting zoom
});
</script>
</body>
</html>
我正在从 Firefox 查看,尝试禁用 CORS 策略,我正在失去信心,天哪,它应该如此简单!非常感谢任何帮助!
编辑:如果我调用 mapzen 或 osm2vectortile 服务会遇到同样的问题,所以 mapbox 不是问题,似乎是一个通用的 cors 问题