【问题标题】:''Uncaught SyntaxError: Unexpected token < in JSON at position'' using openlayers js''Uncaught SyntaxError: Unexpected token < in JSON at position'' 使用 openlayers js
【发布时间】: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


【解决方案1】:

您的 WFS 请求无效。改变

'maxFeatures=50'

'maxFeatures=50&'

您也没有设置 WFS 请求的 BBOX。所以你应该要么删除 bbox 策略,要么添加

&BBOX=' + extent.toString()

到您的 WFS 网址。

【讨论】:

  • 我确实改变了这一切,但我得到了同样的错误。
  • 在您的开发者控制台中查看来自 GeoServer 的响应并检查 XML 中的错误消息。
【解决方案2】:

我遇到了同样的问题,所以我的回答也可能对其他人有所帮助。

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

在我的情况下,这是由于服务器在检索这些特定图层的查询结果时内存不足,因此返回了一个 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>

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

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    相关资源
    最近更新 更多