How to add an HTTP header to openlayers3 requests?

How can I insert an http header into every request on a map layer?

In this case, I need to send an authentication header for this layer and source, but I also want to send other headers.

The search for code and documents appeared without prompts.

+4
source share
1 answer

Answered on github.

By default, the image is downloaded as follows: img.src = ' http://example.com/tile.png '; - that is, we set the src attribute of the image to the image URL. In this case, you cannot set the headers for the request.

, source.setTileLoadFunction(customLoader). , "tile image". . ol.ImageTile URL.

. :

function customLoader(tile, src) {
  var client = new XMLHttpRequest();
  client.open('GET', src);
  client.setRequestHeader('foo', 'bar');
  client.onload(function() {
    var data = 'data:image/png;base64,' + btoa(unescape(encodeURIComponent(this.responseText));
    tile.getImage().src = data;
  });
  client.send();
}
+2

Source: https://habr.com/ru/post/1584292/


All Articles