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

标签 javascript html cors leaflet

我正在使用 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/

相关文章:

javascript - Google 标记管理器未定义

javascript - 通过 highcharts 在 django 中绘制饼图

javascript - 如何在另一个动画正在进行时延迟运行动画

javascript - 将内容向下移动到固定位置标题后面

c# - FFMPEG 转换的视频在 HTML5 中不起作用

reactjs - OPTIONS 请求过多

javascript - Android/iOS WebView 未遵循 CORS HTTP 重定向 (30x)

javascript - React 的 onMouseMove 在 div 上工作但不在自定义组件上工作?

html - 在 css 自定义中使用宽度的问题

java - CORS和错误以及Access-Control-Allow-Origin header 的问题