5

In the past I have used OL extensively in order to make WFS requests to Geoserver and add layer on a map. Now I am trying to learn LeafletJS and I want to do something similar. I have made a WMS request successfully but I can not find documentation of how to do WFS request. This is the WMS request to geoserver:

var layer_name = L.tileLayer.wms("http://something:xxxx/geoserver/wms", {
        layers: 'workspace:geoserver_layer',
        format: 'image/png',
        transparent: true,
        version: '1.1.0',
    });
    layer_name.addTo(map);

Also, how do I solve the problem with the proxy. I remember in OL I used a cgi script.

5
  • Have you seen this question: gis.stackexchange.com/questions/129965/… ?
    – elrobis
    Mar 30, 2016 at 19:16
  • I have made it partially work using this post: stackoverflow.com/questions/25187937/…
    – user1919
    Mar 30, 2016 at 19:22
  • 1
    The problem is i get an error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8888' is therefore not allowed access. When I click the link I see all the response as JSON.
    – user1919
    Mar 30, 2016 at 19:23
  • 2
    Sounds like a cross domain policy request issue. Basically, the page is hosted via one name-server (i.e. localhost, etc), and it is requesting assets from another name-server (i.e. something:xxxx, etc). Does that sound like your current setup? ...honestly I forget how to go about fixing that while testing on localhost. Check out the advice in this blog post ..does it help?
    – elrobis
    Mar 30, 2016 at 20:01
  • 1
    Nope. Not really. I was reading that if I use JSONP I can avoid the proxy solution. I will have to try to configure my geoserver installation to use JSONP and I will let you know.
    – user1919
    Mar 30, 2016 at 20:45

1 Answer 1

5

Ok, if you're getting the 'No 'Access-Control-Allow-Origin' header is present' you have two options:

  1. Enable CORS
  2. Enable JSONP.

Personally, I'd enable both.

JSONP can be enabled with an environment variable, by the way, which makes it nice and easy. On Linux you can do this:

export ENABLE_JSONP=true

And then start Tomcat.

For enabling CORS using Tomcat, I use this snippet:

<filter>
  <filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

That goes in your web.xml file, which is located at $CATALINA_HOME/webapps/geoserver/WEB-INF/web.xml. Note that also in this file is a commented out section that can enable JSONP:

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

Uncomment that and you've got some JSONP format available as a WFS request.

If you have a reverse proxy, you can set up CORS there, just look at the docs.

FYI, the difference between JSON and JSONP is that JSONP is wrapped in a function, which for some reason is interpreted differently than regular JSON.

Here's an example loading a dataset into Leaflet using JSONP: https://github.com/alexgleith/maps-website/blob/master/glenorchy_mtbp.html

5
  • What's your environment? Are you running GeoServer in tomcat?
    – Alex Leith
    Mar 31, 2016 at 7:44
  • Yes. Tomcat6 on Ubuntu machine. But I have a rather old version of geoserver (2.5)
    – user1919
    Mar 31, 2016 at 7:51
  • Well, if you can find the web.xml file and edit it, you'll be sorted! Let me know if I can help you out further.
    – Alex Leith
    Mar 31, 2016 at 7:55
  • Well I found the xml. What is interesting is that there is not an uncommented section of 'Enabling JSONP'. I am thinking if is not supported at all by Geoserver 2.5 or should I add it?
    – user1919
    Mar 31, 2016 at 7:58
  • Try adding it. Then restart tomcat (should be 'sudo service tomcat6 restart') and see what happens! You can always undo your changes.
    – Alex Leith
    Mar 31, 2016 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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