一、问题描述:
Access to fetch at 'http://localhost:8089/geoserver/wfs' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
二、解决方法:
对 Geoserver安装目录下的webapps/geoserver/WEB-INF/web.xml 中的对跨域部分的注释解除就可以了。
参考博客中,说到把jetty-servlets-9.4.12.v20180830.jar (geoserver:本来就有了,在Geoserver安装目录下的lib文件夹内)放到Geoserver安装目录下的webapps/geoserver/WEB-INF/lib 文件夹下,但其实不用。
重启Geoserver 服务。
参考博客:Geoserver 安装及CORS跨域资源访问配置
问题2:<iframe src = "url"> 的url跨域问题
geoserver_ol_美国地图.html?_ijt=tuar890ntiafom37qjbmu40g48:1 Refused to display'http://localhost:8089/geoserver/topp/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&FORMAT=image%2Fpng&TRANSPARENT=true&QUERY_LAYERS=topp%3Astates&LAYERS=topp%3Astates&exceptions=application%2Fvnd.ogc.se_inimage&INFO_FORMAT=text%2Fhtml&FEATURE_COUNT=50&X=50&Y=50&SRS=EPSG%3A4326&STYLES=&WIDTH=101&HEIGHT=101&BBOX=-108.9228504896164%2C33.9697265625%2C-100.0458973646164%2C42.8466796875' in a frame because it set 'X-Frame-Options' to 'sameorigin'.
问题发生的地方就是<iframe src = url >中url的域和html的域不同导致的跨域问题。
注意: 这个问题没办法通过上面一个问题中的解决方法解决,因为当前geoserver环境就是设置成可以跨域的。
博客:in a frame because it set 'X-Frame-Options' to 'sameorigin'
新问题:
报错:X-Frame-Options may only be set via an HTTP header sent along with a document. It may not be set inside <meta>.
解决方法:
最终结论: 当前情境下的问题无法解决,因为我当前项目中的<iframe src = url> 的地方只是这么简单。而关键是:这个url:
html,这个html是由geoserver内部通过代码的方式生成的,所以无法修改<head>: <head http-equiv="X-Frame-Options" content="deny">,除非修改geoserver源代码。
猜想:能否用document.get*byId()获取到当前<iframe>然后再给其中的<head> 添加新属性。感觉不太可能。
所以,这个问题暂时无法解决。