gpt4 book ai didi

javascript - 来自源 'file://' 的图像已被跨源资源共享策略 : 阻止加载

转载 作者:技术小花猫 更新时间:2023-10-29 12:51:14 25 4
gpt4 key购买 nike

我正在使用 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 中不起作用)

enter image description here

你能帮我吗? (而且我所有的服务器都在本地托管。网络服务器, map 服务器......)

最佳答案

一般情况下,一个网站运行的javascript代码无法访问其他网站的资源。但是来自网站的 javascript 应该能够从同一网站访问资源。这叫做 same-origin policy ,并由所有主要浏览器(不仅仅是 Chrome)实现。

另请阅读 https://developer.mozilla.org/en-US/docs/Same-origin_policy_for_file:_URIsDisable same origin policy in Chrome .

最快的解决方案是通过您的 localhost:8080 网站访问图像 - 然后,该网站中的 javascript 将能够访问同一网站中的图像资源。

关于javascript - 来自源 'file://' 的图像已被跨源资源共享策略 : 阻止加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37768518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com