我正在尝试使用OpenLayers 4.6和Angular 5加载WMS图像层,代码如下:
const syr_layer = new ol_layer_Image({
source: new ol_source_ImageWMS({
url: 'serverurl', crossOrigin: 'anonymous', serverType: 'geoserver',
params: { 'LAYERS': 'tst:syr'},
projection: 'EPSG:4326'
});
});
但是它会抛出一个错误:
GET (我的服务器网址) 401 (在SecurityContext中找不到身份验证对象)
我该如何在openlayers发送的GET请求中添加身份验证标头?
req.setRequestHeader("Authorization", "Basic " + window.btoa(user + ":" + pass));
应该改为client.setRequestHeader("Authorization", "Basic " + window.btoa(user + ":" + pass));
。 - krisaoe