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?

Joshua Dickerson
  • 731
  • 3
  • 10
  • 25

1 Answers1

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

  • Just checked the web.xml file and it had already been set to true so this doesn't look like it is the issue. – Joshua Dickerson Apr 03 '15 at 20:50
  • I have applied your setting to a sample server, it works: http://giswebservices.massgis.state.ma.us/geoserver/wfs?request=getfeature&outputFormat=text/javascript&format_options=callback:loadFeatures&version=1.1.0&service=wfs&typename=massgis:GISDATA.EOTROADS_ARC&propertyname=STREET_NAME&CQL_FILTER=BBOX(SHAPE,236481,901934,236557,902003) – boliwe Apr 04 '15 at 07:35
  • Try to get your url on browser firs. – boliwe Apr 04 '15 at 07: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? – Joshua Dickerson Apr 04 '15 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! – Joshua Dickerson Apr 05 '15 at 14:30