环境: 服务器 geoserver2.4.1
开发机访问服务器发布的wms 存在跨域问题,研究两天终于找到解决方案。
主要思路是: 使用jquery的ajax 代替Groserver的request, 然后将wms的请求地址转发到.net的处理程序, 使用.net的处理程序进行跨域处理。
function mouseClick(e) { var layer = new Array(); layer = map.getLayersByName("Simple_Geometry"); if (layer != null) for (var i = 0; i < layer.length; i++) { map.removeLayer(layer[i]); } var wms_url = "http://10.72.4.219:8089/geoserver/xj/wms?"; var params = { REQUEST: "GetFeatureInfo", BBOX: map.getExtent().toBBOX(), WIDTH: map.size.w, HEIGHT: map.size.h, X: e.xy.x, Y: e.xy.y, SERVICE: "WMS", QUERY_LAYERS: map.layers[0].params.LAYERS, INFO_FORMAT: 'text/html', FEATURE_COUNT: 50, Layers: 'xj:cswl_group', Styles: "", format: "application/vnd.ogc.gml", EXCEPTIONS: "application/vnd.ogc.se_xml", // Exception 类型 srs: 'EPSG:4326' }; var domainUrl = "crossDomain.ashx?URL="; var request = parpreUrl(wms_url, params); request = encodeURIComponent(encodeURIComponent(request)); $.post(domainUrl + request, "", function (e) { document.getElementById('nodelist').innerHTML = e; }); OpenLayers.Event.stop(e); } function parpreUrl(url, paramsObj) { for (var k in paramsObj) { url += (k + "=" + paramsObj[k] + "&"); } return url; }
如果使用Groserver.ProxyHost , 会遇到 triggerEvent未定义问题,所以使用了jqueyr的ajax代替 Geoserver.Request ,另外
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; string url = ""; url = context.Request.QueryString["URL"].ToString(); url = context.Server.UrlDecode(url); HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(url); loHttp.Timeout = 10000; // 10 secs loHttp.UserAgent = "Web Client"; HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse(); Encoding enc = Encoding.GetEncoding(65001); StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc); string lcHtml = loResponseStream.ReadToEnd(); context.Response.Write(lcHtml); loWebResponse.Close(); loResponseStream.Close(); }
这样就可以解决 跨域问题