简体   繁体   English

如何在开放层中使用 XMLHttpRequest

[英]How to use XMLHttpRequest with Open Layers

I need to get images from multiple WebMapServers (of my company) with Open Layers (and pure Javascript).我需要使用 Open Layers(和纯 Javascript)从多个 WebMapServer(我公司的)获取图像。 Basically it works.基本上它有效。 Problem is that some servers require HTTP Basic Auth.问题是某些服务器需要 HTTP 基本身份验证。 The OL documentation and a related SO question say that this should be done with a XMLHttpRequest inside an imageLoadFunction: OL 文档和相关的 SO 问题说这应该通过 imageLoadFunction 中的 XMLHttpRequest 来完成:

https://openlayers.org/en/latest/apidoc/module-ol_Image.htmlhttps://openlayers.org/en/latest/apidoc/module-ol_Image.html

How to assign basic authentication header to XMLHTTPREQUEST? 如何将基本身份验证标头分配给 XMLHTTPREQUEST?

At first I want to get images with XMLHttpRequest and without Basic Auth:起初,我想使用 XMLHttpRequest 获取图像而不使用 Basic Auth:

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
    })
});

The imageLoadFunction only works with the line imageLoadFunction 仅适用于该行

image.getImage().src = src;

but not with the commented XMLHttpRequest.但不是带有注释的 XMLHttpRequest。 I think the loaded image must be assigned in the client.onload function, but I'm not sure how to do this.我认为必须在 client.onload 函数中分配加载的图像,但我不确定如何执行此操作。 So how should I use the XMLHttpRequest inside the imageLoadFunction?那么我应该如何在 imageLoadFunction 中使用 XMLHttpRequest 呢?

From the docs :文档

Providing a custom imageLoadFunction can be useful to load images with post requests or - in general - through XHR requests, where the src of the image element would be set to a data URI when the content is loaded.提供自定义 imageLoadFunction 可用于加载带有 post 请求的图像,或者 - 通常 - 通过 XHR 请求,其中在加载内容时图像元素的 src 将设置为数据 URI。

Maybe try something like this:也许尝试这样的事情:

imageLoadFunction: function(image, src) {
  var client = new XMLHttpRequest();
  client.open('GET', src, true);
  client.setRequestHeader( 'Content-Type',   'image/png' );
  client.setRequestHeader( 'Accept', 'image/png' );
  client.responseType = 'blob';
  client.onload(function() {
    const blob = new Blob(client.response);
    const urlData = URL.createObjectURL(blob);
    image.getImage().src = urlData;
  });
  client.send();
},

What it does:它能做什么:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM