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
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– Joshua Dickerson2015年04月03日 20:50:28 +00:00Commented Apr 3, 2015 at 20:50
-
I have applied your setting to a sample server, it works: giswebservices.massgis.state.ma.us/geoserver/…boliwe– boliwe2015年04月04日 07:35:14 +00:00Commented Apr 4, 2015 at 7:35
-
Try to get your url on browser firs.boliwe– boliwe2015年04月04日 07:36:11 +00:00Commented 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?Joshua Dickerson– Joshua Dickerson2015年04月04日 18:18:13 +00:00Commented Apr 4, 2015 at 18:18
-
1Resolved: 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– Joshua Dickerson2015年04月05日 14:30:04 +00:00Commented Apr 5, 2015 at 14:30
Explore related questions
See similar questions with these tags.