报错如下:

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/ogc http://fzzt.fzjhdn.com:10080/geoserver/schemas/wms/1.3.0/exceptions_1_3_0.xsd">
    <ServiceException code="LayerNotQueryable" locator="QUERY_LAYERS">
      Either no layer was queryable, or no layers were specified using QUERY_LAYERS
</ServiceException>
</ServiceExceptionReport>

代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
var view = map.getView();
var viewResolution = view.getResolution();
var source = requestLayer.getSource();
var url = source.getFeatureInfoUrl(
evt.coordinate, viewResolution, view.getProjection(), {
    'INFO_FORMAT': 'application/json',
    'FEATURE_COUNT': 50
});
if (url) {
    console.log("url:",url)
    $.ajax({
        url: url,
        type: 'GET',
        async: true,
        contentType: 'application/json;charset=utf-8',
        success: data => {
        	console.log("data:",data)
        	console.log("length:",data.features.length)
        }
    })
}

解决方法:

看了下官方文档,他说如果没有提供QUERY_LAYERS,那么将使用layers参数中指定的layers。

image-20230618134021573

之前用写死的图层做的时候是不需要提供这个的,但是改成动态添加图层之后没有这个就会报错,原因未知,那就只好加上这个了,无奈,只好在服务的json文件里再加上一个这个layer属性了

image-20230618134024953