【发布时间】:2017-05-20 16:53:08
【问题描述】:
这几天我试图解决这个问题,但我无法得到正确的答案;我正在尝试使用 openlayers 工具从 geoserver 插入带有 wfs 请求的地图,但出现此错误:
Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Jl (ol.js:342)
at wm.k.Pa (ol.js:342)
at T.<anonymous> (ol.js:340)
这是我的代码:
<HTML>
<head>
<link rel="stylesheet" href="https://openlayers.org/en/v4.1.1/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.1.1/build/ol.js"></script>
</head>
<center>
<div id="mapid" style="width: 600px; height: 500px;"></div>
</center>
<script>
var vectorSource = new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: function(extent) {
return 'http://localhost:8080/geoserver/opengeo/ows?service=WFS&' +
'version=1.0.0&request=GetFeature&typeName=opengeo:comgeo&' +
'maxFeatures=50'+
'outputFormat=application%2Fjson' ;
},
strategy: ol.loadingstrategy.bbox
});
var map = new ol.Map({
layers: [
new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(0, 0, 255, 1.0)',
width: 2
})
})
})
],
target: document.getElementById('mapid'),
view: new ol.View({
center: [-9, 30],
maxZoom: 19,
zoom: 5
})
});
</script>
</HTML>
我不知道问题出在哪里,因为我没有发现任何与 json 问题和 openlayers 相关的问题...谢谢
【问题讨论】:
-
嘿,您能否上传您的应用程序在访问此 url 时得到的响应? localhost:8080/geoserver/opengeo/…
-
@OliverBarnwell 它给了我地图文件的 geojson farmat。
-
{"type":"FeatureCollection".....
-
是否可以创建一个示例 Jsfiddle 或任何地方......我们可以在其中重现相同的错误或查看您迄今为止尝试过的任何内容?
标签: openlayers geojson geoserver