[GIS] Request JSONP from Geoserver using AJAX request in Openlayers 3

ajaxgeojsongeoserveropenlayerswfs

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?

Best Answer

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

Related Question