14

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

  • 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 9 '13 at 3:05
  • It's not made obvious in the resources out there that this needs to be done. Here is the doco atm: docs.geoserver.org/latest/en/user/services/wfs/… – user1567453 Jun 8 '15 at 13:26
32

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.

  • 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): 127.0.0.1:8080/geoserver/… – ca0v Nov 6 '13 at 15:28
  • 1
    @ca0v this works for WFS only. WMS doesn't return jsonp, AFAIK. – Alex Leith Feb 21 '14 at 3:47
  • Apparently, according to osgeo-org.atlassian.net/browse/…, 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'
        });
    },
  • 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 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.