场景

GeoServer简介、下载、配置启动、发布shapefile全流程(图文实践):

上面安装Geoserver的基础下。

使用ajax请求GeoJson时提示跨域

Geoserver中跨域问题解决_geoserver

注:

博客: 霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、ajax请求代码示例

var url = "你的url";

//ajax调用
$.ajax({
url:url,
dataType: 'json',
outputFormat:'text/javascript',
success:function(data){
searchPolygon.addData(data);
},
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus+";"+errorThrown);
},
});

2、Geoserver默认是不支持跨域的,来到Geoserver的安装目录下

webapps/geoserver/WEB-INF/web.xml

编辑该文件

Geoserver中跨域问题解决_程序猿_02

3、放开如下两个地方的CORS配置

Geoserver中跨域问题解决_ajax_03

 

Geoserver中跨域问题解决_跨域_04

4、重启geoserver的服务即可。