Monday 29 July 2019

GetFeatureInfo Request with GeoServer and OpenLayers.loadURL Not Working


I'm making a simple GetFeatureInfo request against a GeoServer WMS, but it returns empty responses. The request utility I'm using is OpenLayers.loadURL.


The funny thing is, I can paste the generated request URL from FireBug into a new browser tab and get a valid response. So, it seems to be something with the OpenLayers.loadURL. See the examples below.


I can get a valid response with this map event where the GetFeatureInfo request is in a new window:


  map.events.register('click', map, function (e) {
var url = "http://localhost:8080/geoserver/MYLAYER/wms"
var params = ""

+ "REQUEST=GetFeatureInfo"
+ "&SERVICE=WMS"
+ "&EXCEPTIONS=application/vnd.ogc.se_xml"
+ "&VERSION=1.1.1"
+ "&BBOX=" + map.getExtent().toBBOX()
+ "&X=" + Math.round(e.xy.x)
+ "&Y=" + Math.round(e.xy.y)
+ "&INFO_FORMAT=text/html"
+ "&QUERY_LAYERS=NERD:NERD Risk Rates"
+ "&LAYERS=NERD:NERD Risk Rates"

+ "&FEATURE_COUNT=50"
+ "&format=image/png"
+ "&SRS=EPSG:900913"
+ "&STYLES="
+ "&WIDTH=" + map.size.w
+ "&HEIGHT=" + map.size.h;

console.log(url+"?"+params);
window.open(url+"?"+params,
"getfeatureinfo",

"location=0,status=0,scrollbars=1,width=600,height=150"
);

});

However, this map event using OpenLayers.loadURL always returns an empty response and no error. But if I cut and paste the GET request url from FireBug into a new browser window, I get the proper response:


  map.events.register('click', map, function (e) {
var url = "http://localhost:8080/geoserver/MYLAYER/wms"

var paramHash = {

REQUEST : "GetFeatureInfo" ,
EXCEPTIONS : "application/vnd.ogc.se_xml" ,
SERVICE : "WMS" ,
VERSION : "1.1.1" ,
BBOX : map.getExtent().toBBOX() ,
X : Math.round(e.xy.x) ,
Y : Math.round(e.xy.y) ,
INFO_FORMAT : "text/html" ,
QUERY_LAYERS : "NERD:NERD Risk Rates" ,
LAYERS : "NERD:NERD Risk Rates" ,

FEATURE_COUNT : 50 ,
format : "image/png" ,
srs : "EPSG:900913" ,
styles : "" ,
WIDTH : map.size.w ,
HEIGHT : map.size.h
};

OpenLayers.loadURL(url, paramHash, this, setHTML, setHTML);
OpenLayers.Event.stop(e);


});

Someone have any ideas?



Answer



Sounds like a same origin problem to me. Are you serving your page from a different host/port combination than the map server? See http://trac.osgeo.org/openlayers/wiki/FrequentlyAskedQuestions#ProxyHost for details of how to fix this using a proxy or move your html page to data/web in your geoserver installation.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...