【发布时间】:2021-01-18 05:35:21
【问题描述】:
我需要使用 Open Layers(和纯 Javascript)从(我公司的)多个 WebMapServers 获取图像。 基本上它有效。问题是某些服务器需要 HTTP 基本身份验证。 OL 文档和相关的 SO 问题说这应该使用 imageLoadFunction 中的 XMLHttpRequest 来完成:
https://openlayers.org/en/latest/apidoc/module-ol_Image.html
How to assign basic authentication header to XMLHTTPREQUEST?
一开始我想获取带有 XMLHttpRequest 而没有基本身份验证的图像:
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Image({
source: new ol.source.ImageWMS({
ratio: 1,
params: { LAYERS: 'ENC', CSBOOL: '2083', CSVALUE: ',,,,,3'},
url: 'https://wms-without-basic-auth.com/?',
imageLoadFunction: function(image, src) {
image.getImage().src = src;
/*
var client = new XMLHttpRequest();
client.open('GET', src, true);
client.setRequestHeader( 'Content-Type', 'image/png' );
client.setRequestHeader( 'Accept', 'image/png' );
client.onload(function() {
image.getImage().src = src;
});
client.send();
*/
},
})
})
],
view: new ol.View({
center: ol.proj.fromLonLat([6,54]),
zoom: 6
})
});
imageLoadFunction 仅适用于该行
image.getImage().src = src;
但不是带有注释的 XMLHttpRequest。 我认为加载的图像必须在 client.onload 函数中分配,但我不确定如何执行此操作。 那么 imageLoadFunction 里面的 XMLHttpRequest 应该如何使用呢?
【问题讨论】:
-
回复说什么?是有错误,还是根本没有处理?
标签: javascript openlayers wms