Loading WMS in Vuejs with OpenLayers

1.2k Views Asked by At

I am trying to read a WMS layer from a local host Geoserver in Vue.js. I am running Geoserver on a different port than my vue.js.

How I am supposed to load in Vue JS my WMS Layer like in this example: 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>

In my browser : Access to XMLHttpRequest at 'http://192.168.1.23:3000/sockjs-node/info?t=1578388235952' from origin 'http://localhost:3000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

1

There are 1 best solutions below

0
On

You have a CORS issue (CORS stands for Cross-Origin Resource Sharing). You need to enable CORS on your server.

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"
  }
}

Some good resources for enabling CORS could be found here:

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

If you have a PHP backend you can just include the following header:

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