【发布时间】:2018-06-17 12:55:27
【问题描述】:
<script type="text/javascript">
var vectorSource = new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: "http://localhost/cgi-bin/mapserv.exe?map=D:\MS4W\Apache\htdocs\baseWFS.map&SERVICE=WFS&VERSION=1.0.0&REQUEST=getfeature&TYPENAME=parcel&bbox=518259.145000,3240058.724100,519352.395700,3241604.305400,EPSG:4326&outputformat=geojson",
strategy: ol.loadingstrategy.all
});
var vector = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(0, 0, 0, 1.0)',
width: 2
})
})
});
var map = new ol.Map({
layers: [vector],
view: new ol.View({
center: [518744, 3240850],
maxZoom: 10,
zoom : 1
}),
target: document.getElementById('map')
});
我写了这个 js 代码,但是它没有显示任何东西并且有这个错误: 跨域请求被阻止:同源策略不允许读取位于http://localhost/cgi-bin/mapserv.exe?map=D:MS4WApachehtdocsLIS_project%08aseWFS.map&SERVICE=WFS&VERSION=1.0.0&REQUEST=getfeature&TYPENAME=parcel&bbox=518259.145000,3240058.724100,519352.395700,3241604.305400,EPSG:4326&outputformat=geojson 的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。
【问题讨论】:
-
让我知道完整的代码,我会帮你添加 CORS 标头,我的意思是你是如何发出请求的,任何客户端都喜欢“请求”?请提供有关客户端和服务器的完整信息。
-
您是否有机会通过
file://url 运行此脚本?该协议不允许使用 CORS。如果您对两者都使用 localhost(或者将所有内容保留在同一个域名上),您将不会遇到此问题。