1

I am creating a web application using OpenLayers 3 and I am looking to import my vector layers into this. I currently have my data in Geoserver and I am trying to use the following code, but when I make the below request using jQuery:

    var vectorLoader = function(extent, resolution, projection) {
    var url = 'http://XXX185:8080/geoserver/wfs?service=WFS&' +
        'version=1.1.0&request=GetFeature&typename=YYY:QGIS&' +
        'outputFormat=text/javascript&format_options=callback:loadFeatures' +
        '&srsname=EPSG:3857&bbox=' + extent.join(',') + ',EPSG:3857';
    $.ajax({
      url: url,
      dataType: 'jsonp'
    });
  };

  var loadFeatures = function(response) {
    var features = vectorSource.readFeatures(response);
    vectorSource.addFeatures(features);
  };

  var vectorSource = new ol.source.ServerVector({
    format: new ol.format.GeoJSON(),
    loader: vectorLoader,
    strategy: ol.loadingstrategy.createTile(new ol.tilegrid.XYZ({
      maxZoom: 19
    }))
  });

  var serverVector = new ol.layer.Vector({
    source: vectorSource,
    style: vectorStyle
  });

I get this error:

Uncaught SyntaxError: Unexpected token <
jquery-latest.min.js:4 Resource interpreted as Script but transferred with MIME type application/xml: "http://XXXpc185:8080/geoserver/wfs?service=WFS&version=1.1.0&request=GetFea…SG:3857&callback=jQuery111109427054924890399_1428089051875&_=1428089051876".

I imagine it is something to do with the way the vectorLoader is requesting the WFS?

1 Answer 1

2

Did you enabled geoserver JSONP settings in web.xml

/geoserver/WEB-INF/web.xml

contains settings like this:

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

You should change ENABLE_JSONP as true

5
  • Just checked the web.xml file and it had already been set to true so this doesn't look like it is the issue. Apr 3, 2015 at 20:50
  • I have applied your setting to a sample server, it works: giswebservices.massgis.state.ma.us/geoserver/…
    – boliwe
    Apr 4, 2015 at 7:35
  • Try to get your url on browser firs.
    – boliwe
    Apr 4, 2015 at 7:36
  • Glad to see the settings work, still seems like I haven't got something set up correctly yet, i may need to re-write the wat the vector loader requests the json? Apr 4, 2015 at 18:18
  • 1
    Resolved: After further investigation, the JSONP settings within the web.xml file were not exactly correct and following the answer by @Bayram this now works. Thanks! Apr 5, 2015 at 14: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.