我正在使用 leaflet-image.js 从传单 map 创建图像。用于创建图像的代码是 https://github.com/mapbox/leaflet-image 中示例中的代码。即
var map = L.mapbox.map('map', 'YOUR.MAPID').setView([38.9, -77.03], 14);
leafletImage(map, function(err, canvas) {
// now you have canvas
// example thing to do with that canvas:
var img = document.createElement('img');
var dimensions = map.getSize();
img.width = dimensions.x;
img.height = dimensions.y;
img.src = canvas.toDataURL();
document.getElementById('images').innerHTML = '';
document.getElementById('images').appendChild(img);
});
问题是图像似乎被某些 CORS 安全功能阻止了。下面是 Google Chrome 控制台的图像(不是 enevn 在 firefox 中不起作用)
你能帮我吗? (而且我所有的服务器都在本地托管。网络服务器, map 服务器......)
最佳答案
一般情况下,一个网站运行的javascript代码无法访问其他网站的资源。但是来自网站的 javascript 应该能够从同一网站访问资源。这叫做 same-origin policy ,并由所有主要浏览器(不仅仅是 Chrome)实现。
另请阅读 https://developer.mozilla.org/en-US/docs/Same-origin_policy_for_file:_URIs和 Disable same origin policy in Chrome .
最快的解决方案是通过您的 localhost:8080
网站访问图像 - 然后,该网站中的 javascript 将能够访问同一网站中的图像资源。
关于javascript - 来自源 'file://' 的图像已被跨源资源共享策略 : 阻止加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37768518/