0

I want to fetch all the layer names from GeoServer using AJAX and append the data in the HTML dropdown. I used xampp server and file in .php extension, Here is my code :

HTML code :

<label for="layer">Select Layer</label>
<select class="form-control" id="layer" name="layer">
<option value="">Select Layer</option>
</select>

Jquery code :

$(document).ready(function(){
        $.ajax({
        type: &quot;GET&quot;,

        url: &quot;http://localhost:8080/geoserver/wfs?request=getCapabilities&quot;,

        dataType: &quot;xml&quot;,
        success: function(xml) {

            var select = $('#layer');
            $(xml).find('FeatureType').each(function(){

                var name = $(this).find('Name').text();

                    $(this).find('Name').each(function(){
                    var value = $(this).text();
                    select.append(&quot;&lt;option class='ddindent' value='&quot;+ value +&quot;'&gt;&quot;+value+&quot;&lt;/option&gt;&quot;);
                });
            });

        }
    });
});

but it gives the below error in the console window :

Access to XMLHttpRequest at 'http://localhost:8080/geoserver/wfs?request=getCapabilities' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
TomazicM
  • 25,601
  • 22
  • 29
  • 39
  • turn cors on in Jetty or tomcat (https://gis.stackexchange.com/questions/210109/enabling-cors-in-geoserver-jetty) – Ian Turton Oct 06 '21 at 10:24
  • but I am using xampp server and I used PHP code then what is the solution. – Taranpreet Singh Oct 06 '21 at 11:08
  • See https://stackoverflow.com/questions/34872760/how-do-i-enable-cross-origin-resource-sharing-on-xampp – TomazicM Oct 06 '21 at 11:31
  • I wrote the code: Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" Header set Access-Control-Allow-Headers "X-Requested-With, Content-Type, X-Token-Auth, Authorization" but it gives the same error. – Taranpreet Singh Oct 08 '21 at 04:56

0 Answers0