【问题标题】:How to add a http header to openlayers3 requests?如何向 openlayers3 请求添加 http 标头?
【发布时间】:2015-10-02 19:07:14
【问题描述】:

如何在每个地图图层请求中注入 http 标头?

在这种情况下,我需要为给定的层和源发送一个 Authentication 标头,但我可能还想发送其他标头。

对代码和文档的搜索没有任何线索。

【问题讨论】:

    标签: openlayers-3


    【解决方案1】:

    Answered on github.

    默认情况下,图片加载如下: img.src = 'http://example.com/tile.png'; - 即我们将一个Image的src属性设置为图片的url。在这种情况下,您没有机会为请求设置标头。

    您可以通过调用 source.setTileLoadFunction(customLoader) 来覆盖此行为。这假设您正在使用“平铺图像”源。然后,您有责任定义自定义加载器。这个函数会被一个 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多