16

I'd like to enable jsonp on GeoServer 2.3.0. The docs say to set ENABLE_JSONP to true.

A response from the GeoServer users list says to edit web.xml, (set System variable ENABLE_JSONP=true and outFormat=text/javascript )but I have no idea which xml tags to use and where to put it.

Here are installed versions: GeoServer 2.3.0 (Web Archive)has been installed on a remote host with tomcat 7.0.39

geomajor56
  • 2,102
  • 2
  • 18
  • 26
  • Can you update the question to provide a link to the "docs"? You might also like to explain the specific source of your GeoServer install (e.g. from source, distro packages, some windows installer, etc). – BradHards Apr 09 '13 at 03:05
  • It's not made obvious in the resources out there that this needs to be done. Here is the doco atm: http://docs.geoserver.org/latest/en/user/services/wfs/outputformats.html – user1567453 Jun 08 '15 at 13:26

2 Answers2

37

I got some help on the GeoServer users list.

Add to this file: /tomcat/webapps/geoserver/WEB-INF/web.xml

<context-param>
    <param-name>ENABLE_JSONP</param-name>
    <param-value>true</param-value>
</context-param>

and restart the service. Then, in your json request use format=text/javascript

Here's the code I used to request the WFS features from GeoServer. I'm using the Leaflet API along with jquery.

var rootUrl = 'http://tomcat.capecodgis.com/geoserver/capecodgis/ows';

var defaultParameters = {
    service: 'WFS',
    version: '1.0.0',
    request: 'GetFeature',
    typeName: 'capecodgis:monitor_station',
    maxFeatures: 200,
    outputFormat: 'text/javascript',
    format_options: 'callback: getJson'

};

var parameters = L.Util.extend(defaultParameters);

$.ajax({
    url: rootUrl + L.Util.getParamString(parameters),
    dataType: 'jsonp',
    jsonpCallback: 'getJson',
    success: handleJson
});


function handleJson(data) {
    L.geoJson(data, {
        onEachFeature: onEachFeature,
        pointToLayer: function (feature, latlng) {
            return L.circleMarker(latlng, geojsonMarkerOptions);
            //return L.marker(latlng);
        }
    }).addTo(map);
}

Hopes this gets you started and feel free to ask for more info.

geomajor56
  • 2,102
  • 2
  • 18
  • 26
  • This if for WMS only? If not, can you provide an example endpoint? I'm trying to use JSONP for a WFS service but can't get it working (Failed to find response for output format jsonp):

    http://127.0.0.1:8080/geoserver/wfs?request=GetFeature&version=1.1.0&typeName=topp:states&propertyName=STATE_NAME,PERSONS&BBOX=-110.29075,22.723749999999995,-81.41024999999999,51.60425000000001,EPSG:4326&OUTPUTFORMAT=jsonp

    – ca0v Nov 06 '13 at 15:28
  • 1
    @ca0v this works for WFS only. WMS doesn't return jsonp, AFAIK. – Alex Leith Feb 21 '14 at 03:47
  • Apparently, according to https://osgeo-org.atlassian.net/browse/GEOS-5749?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&showAll=true, setting the context parameter did not work in Geoserver 2.3.0. I've wasted half a day chasing that down. I really need to get an up-to-date Geoserver running... – Auspex Jan 26 '17 at 14:22
3

In order to improve readability you can also put the params directly into the AJAX call.

With the JSONP call you won't need the format_options or the success parameter. The callback function will be set with jsonpCallback parameter on the AJAX call and the format_options will be set with jsonp:'format_options'.

    $.ajax('http://demo.opengeo.org/geoserver/wfs',{
        type: 'GET',
        data: {
            service: 'WFS',
            version: '1.0.0',
            request: 'GetFeature',
            typeName: 'capecodgis:monitor_station',
            maxFeatures: 200,
            outputFormat: 'text/javascript',
            request: 'GetFeature',
            srsname: 'EPSG:3857',
            bbox: extent.join(',') + ',EPSG:3857'
            },
        dataType: 'jsonp',
        jsonpCallback:'callback:handleJson',
        jsonp:'format_options'
        });
    },
Dennis Bauszus
  • 2,289
  • 3
  • 21
  • 31
  • Nice example but I didn't pick up on it immediately. The docs state that you can change the callback name via the "format_options" query string using the "callback" options. This example changes the default callback to "tests": [path_to_wfs_service]?format_options=callback:test – ca0v May 19 '18 at 03:44