如何在openlayers3请求中添加http标头?
如何在每个地图图层请求中注入http标头?
How can I inject a http header into every map layer request?
在这种情况下,我需要为给定的层和源发送一个Authentication标头,但是我可能也想发送其他标头.
In this case, I need to send an Authentication header for a given layer and source, but I may want to send other headers too.
对代码和文档的搜索没有任何线索.
A search of the code and docs came up with no clues.
默认情况下,图像加载是这样的:img.src =' http://example.com/tile. png '; -也就是说,我们将Image的src属性设置为image url.在这种情况下,您将没有机会设置请求的标题.
By default, image loading happens like this: img.src = 'http://example.com/tile.png'; - that is, we set the src attribute of an Image to the image url. In this case, you don't have an opportunity to set the headers for the request.
您可以通过调用source.setTileLoadFunction(customLoader)来覆盖此行为.假设您正在使用平铺图像"源.然后,您有责任定义自定义加载程序.该函数将使用ol.ImageTile和字符串URL进行调用.
You can override this behavior by calling source.setTileLoadFunction(customLoader). This assumes you are working with a "tile image" source. Then it is your responsibility to define the custom loader. This function will get called with an ol.ImageTile and a string URL.
其余的取决于您.您的自定义加载程序可能看起来像这样:
The rest is up to you. Your custom loader might look something like this:
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();
}