1

I'm creating a geoportal, which works only on my localhost. I have a problem with adding to my ol.map a vector layer from Geoserver using WFS. I'm using Geoserver v2.7, OL v3.15.1 and jQuery v2.2.4. I had a cross-domain problem, since my gesoerver is hosted at 8080 port and the app is displayed using 9080 port, so I decided to add this layer using JSONP. I added to geoserver\WEB-INF\web.xml the following code:

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

Here's my code for adding a layer through AJAX request:

   var vectorLoader = function() {
   $.ajax('http://localhost:8080/geoserver/ows',{
     type: 'GET',
     data: {
      service: 'WFS',
      version: '2.0.0',
      request: 'GetFeature',
      typename: 'brzesko:wydarzenia',
      srsname: 'EPSG:3857',
      outputFormat: 'application/json',
      bbox: '2293539.9663390242,6439346.050046678,2294549.5570354667,6442264.769222975'
     },
     dataType: 'jsonp',
     jsonpCallback: 'loadFeatures',
     jsonp:'format_options'
     });
   };
  var geoJSON = new ol.format.GeoJSON();  
  var loadFeatures = function(response) {
   vectorSource.addFeatures(geoJSON.readFeatures(response));
  };
  var vectorSource = new ol.source.Vector({
    format: geoJSON,
    loader: vectorLoader
  });

  var wydarzenia= new ol.layer.Vector({
    source: vectorSource (...)

(can't use outputFormat:'text/javascript', my WFS doesn't support it...) Then I get an error in the console.log:

Uncaught SyntaxError: Unexpected token : http://localhost:8080/geoserver/ows?(...)

However, when I open the link in new tab I get a working JSON with all features and information. Does anybody know how to fix this? I've seen several similar posts but none of the given solutions works for me.

Your Answer

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

Browse other questions tagged or ask your own question.