在上一篇博文中详细介绍了跨域问题的解决方案,跨域问题详解
ArcGIS for JS 3.x作为调用方使用代理来处理跨域问题,同时也可以发送POST请求解决GET请求最大2048个字符的限制问题。
官方代理设置指南:https://developers.arcgis.com/javascript/3/jshelp/ags_proxy.html
-
2、添加依赖
//引入Spring boot 内嵌的Tomcat对jsp的解析包 compile('org.apache.tomcat.embed:tomcat-embed-jasper') compile('javax.servlet:javax.servlet-api:4.0.1') compile('javax.servlet:jsp-api:2.0') compile('javax.servlet:jstl')
-
3、设置代理文件的地址
require(["esri/config"], function (esriConfig) {
//代理配置
esriConfig.defaults.io.proxyUrl = "/demo/proxy.jsp";//指定代理文件在项目中的实际位置
esriConfig.defaults.io.alwaysUseProxy = false;
});
-
4、配置需要代理的请求路径
在proxy.jsp文件会从classpath中(即classes目录下)找proxy.config文件,当alwaysUseProxy 设置为false时,只有与proxy.config中匹配的url地址才会使用代理。
proxy.config配置:
serverUrl 标签中url属性配置要代理的地址,matchAll属性是否全匹配。另外还可以配置代理日志输出路径和日志级别。<?xml version="1.0" encoding="utf-8" ?> <ProxyConfig allowedReferers="*" logFile="proxy_log.log" logLevel="INFO" mustMatch="true"> <serverUrls> <serverUrl url="http://services.arcgisonline.com" matchAll="true"/> <serverUrl url="http://localhost:8080/geoserver/ows" matchAll="true"/> <serverUrl url="http://localhost:8080/geoserver/wms" matchAll="true"/> <serverUrl url="http://localhost:8080/geoserver/wfs" matchAll="true"/> </serverUrls> </ProxyConfig> <!-- See https://github.com/Esri/resource-proxy for more information -->