【问题标题】:Loading WMS in Vuejs with OpenLayers使用 OpenLayers 在 Vuejs 中加载 WMS
【发布时间】:2020-01-07 09:01:04
【问题描述】:

我正在尝试从 Vue.js 中的本地主机 Geoserver 读取 WMS 层。我在与 vue.js 不同的端口上运行 Geoserver。

我应该如何在 Vue JS 中加载我的 WMS 层,如下例所示:https://vuelayers.github.io/#/docs/component/tile-layer

    <template>
      <vl-map :load-tiles-while-animating="true" :load-tiles-while-interacting="true" data-projection="EPSG:4326" style="height: 400px">
        <vl-view :zoom.sync="zoom" :center.sync="center" :rotation.sync="rotation"></vl-view>

        <vl-layer-tile>
          <vl-source-sputnik></vl-source-sputnik>
        </vl-layer-tile>

        <vl-layer-tile id="wmts">
          <vl-source-wmts :attributions="attribution" :url="url" :layer-name="layerName" :matrix-set="matrixSet" :format="format" 
                          :style-name="styleName"></vl-source-wmts>
        </vl-layer-tile>
      </vl-map>
    </template>

    //////////////////////////////// For WMS SOURCES 
<script>
  export default {
    data () {
      return { 
        zoom: 4,
        center: [50, 40],
        rotation: 0,
        cmp: 'vl-source-wms',
        url: 'http://localhost:8081/geoserver/cite/wms',
        layers: 'cite:vnm_polbnda_adm3_2014_pdc',
        extParams: { TILED: true },
        serverType: 'geoserver',
      }
    },
  }
</script>

在我的浏览器中:从源“http://localhost:3000”访问“http://192.168.1.23:3000/sockjs-node/info?t=1578388235952”处的 XMLHttpRequest 已被 CORS 策略阻止:响应中的“Access-Control-Allow-Origin”标头的值不得为当请求的凭据模式为“包含”时,通配符“*”。 XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。

【问题讨论】:

    标签: vue.js openlayers vuelayers


    【解决方案1】:

    您遇到了 CORS 问题(CORS 代表跨域资源共享)。您需要在服务器上启用 CORS。

    devServer: {
      ...
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
        "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
      }
    }
    

    可以在此处找到一些启用 CORS 的好资源:

    https://scotch.io/courses/build-an-online-shop-with-vue/enabling-cors

    如果您有 PHP 后端,则只需包含以下标头:

    header(“Access-Control-Allow-Origin: *”);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多