如何将HTTP标头添加到openlayers4请求? [英] How to add a http header to openlayers4 requests?

查看:1035
本文介绍了如何将HTTP标头添加到openlayers4请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些wms或wfs源需要用户和密码身份验证. 例如 https://apps.sogelink.fr/maplink/public/wfs? request = GetCapabilities 需要基本身份验证. 如何注入此身份验证?

some wms or wfs sources require user and password authentication. for example https://apps.sogelink.fr/maplink/public/wfs?request=GetCapabilities need Basic authentication. How can I inject this authentication?

推荐答案

您可以将自己的imageLoadFunction提供给

You can provide your own imageLoadFunction to an ImageWMS source. The default one just takes the URL and inserts it as the src of the img tag:

ol.source.Image.defaultImageLoadFunction = function(image, src) {
  image.getImage().src = src;
};

这个问题是已经在OpenLayers GitHub上问到了,是那里的一个例子:

That question was already asked on the OpenLayers GitHub, here is an example from there:

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

OpenLayers的文档真的很好.只需找到一个使用所需功能的示例,然后单击指向API文档的链接即可.

The documentation of OpenLayers is really good. Just find an example that uses features you want and then follow the links to the API docs.The ol.source.Vector doc even includes an example of a loading function for WFS, where you could manipulate the request:

new ol.source.Vector({
  format: new ol.format.GeoJSON(),
  loader: function(extent, resolution, projection) {
    var wfsUrl = 'TODO';
    var xhr = new XMLHttpRequest();
    // see above example....
  }
}); 

这篇关于如何将HTTP标头添加到openlayers4请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆