问题描述
我正在研究OpenLayers和Geoserver。 当用户单击功能时,我已经创建了一个制表器表来获取功能信息数据。 我的问题是我需要将Geoserver的html响应转换为JSON。我该怎么办?
这是Geoserver的回复
<table class="featureInfo">
<caption class="featureInfo">l1Data</caption>
<tbody><tr>
<th>id</th>
<th>sensor</th>
<th>timestamp</th>
<th>reliability</th>
</tr>
<tr>
<td>16021</td>
<td>Basic</td>
<td>Sep 18,2020 12:05:02 PM</td>
<td>0.929717403858748</td>
</tr>
</tbody>
</table>
制表符已设置为:
table = new Tabulator("#eventtable",{
//height:"311px",layout:"fitColumns",columns:[
{title:"ID",field:"id",visible:false},{title:"Sensor",field:"sensor",sorter:"string"},{title:"Last Detection",field:"timestamp",sorter:"date"},{title:"Reliability",field:"reliability",sorter:"number"},],});
已编辑 我发现我可以收到一个application / json响应,但是不能放入Tabulator。点击地图上的代码:
map.on('singleclick',function (evt) {
var viewResolution = /** @type {number} */ (view.getResolution());
var url = wmsSource.getfeatureinfoUrl(
evt.coordinate,viewResolution,'epsg:3857',{ 'INFO_FORMAT': 'application/json' });
if (url) {
fetch(url)
.then(function (response) { return response.text(); })
.then(function (html) {
var test1 = JSON.parse(html);
var test2 = test1.features[0].properties;
table.setData(test2);
});
}
});
错误是:
tabulator.min.js:2 Data Loading Error - Unable to process data due to invalid data type
Expecting: array
Received: object
解决方法
您无需弄清楚如何进行转换,只需让GeoServer将信息作为JSON返回即可。您可以通过将info_format
参数设置为application/json
来实现。 GeoServer manual提供了可用于控制响应的所有可能参数和格式的完整详细信息。