'';未捕获的语法错误:意外标记<;在位置'';使用openlayers js

'';未捕获的语法错误:意外标记<;在位置'';使用openlayers js,openlayers,geojson,geoserver,Openlayers,Geojson,Geoserver,这是几天来我试图解决这个问题,但我不能得到正确的答案;我试图使用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.<anonymou

这是几天来我试图解决这个问题,但我不能得到正确的答案;我试图使用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)
Uncaught SyntaxError:JSON中位置0处的意外标记<
在JSON.parse()处
在Jl(ol.js:342)
在wm.k.Pa(ol.js:342)
at T.(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>

var vectorSource=新的ol.source.Vector({
格式:新建ol.format.GeoJSON(),
url:函数(范围){
返回'http://localhost:8080/geoserver/opengeo/ows?service=WFS&' +
'version=1.0.0&request=GetFeature&typeName=opengeo:comgeo&'+
“maxFeatures=50”+
'outputFormat=应用程序%2Fjson';
},
策略:ol.loadingstrategy.bbox
});
var map=新ol.map({
图层:[
新ol.layer.Vector({
来源:矢量源,
风格:新的ol风格({
笔划:新的ol风格笔划({
颜色:“rgba(0,0,255,1.0)”,
宽度:2
})
})
})
],
目标:document.getElementById('mapid'),
视图:新ol.view({
中心:[-9,30],
maxZoom:19,
缩放:5
})
});

我不知道问题出在哪里,因为我没有发现任何json问题和openlayers。。。谢谢

您的WFS请求无效。改变

'maxFeatures=50'

您也没有设置WFS请求的BBOX。因此,您要么删除bbox策略,要么添加

&BBOX=' + extent.toString()

到您的WFS url。

我有一个相同的问题,因此我的答案也可能对其他人有所帮助

事实证明,我请求的某些层(但不是全部)确实返回了XML而不是JSON。这很难调试,因为我在一个循环中请求多个层,有些层会按预期返回JSON,有些则不会,这让您认为它们都很好,因为您会看到地图上显示的一些层

在我的例子中,这是由于服务器在检索这些特定层的查询结果时内存不足,因此返回了一个XML错误页面,如下所示:

<ServiceExceptionReport xmlns="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2.0" xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd">
<ServiceException>
java.lang.RuntimeException: java.io.IOException java.io.IOExceptionorg.postgresql.util.PSQLException: Ran out of memory retrieving query results. Ran out of memory retrieving query results. Java heap space
</ServiceException>
</ServiceExceptionReport>

java.lang.RuntimeException:java.io.IOException java.io.IOException org.postgresql.util.PSQLException:检索查询结果时内存不足。检索查询结果时内存不足。Java堆空间

我建议直接在web浏览器中加载所有层的URL,或者使用浏览器开发人员工具检查每个层的服务器响应,即使您非常确定它们应该返回JSON。

嘿,您是否也可以上载应用程序点击此URL时得到的响应@OliverBarnwell它给了我地图文件的geojson farmat。{“type”:“FeatureCollection”..…可以创建一个示例JSFIDLE或任何地方..在那里我们可以复制相同的错误或查看您迄今为止尝试过的任何内容?我确实更改了所有这些,但我得到了相同的错误。在开发人员控制台中查看来自GeoServer的响应,并检查XML中的错误消息。
<ServiceExceptionReport xmlns="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2.0" xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd">
<ServiceException>
java.lang.RuntimeException: java.io.IOException java.io.IOExceptionorg.postgresql.util.PSQLException: Ran out of memory retrieving query results. Ran out of memory retrieving query results. Java heap space
</ServiceException>
</ServiceExceptionReport>