0

I know this issue have been discussed well on this topic, but I can't seem to find how to apply this to my function. I keep getting undefined variables logs.

function requestLayersFromGeoserver() {

    var parser = new ol.format.WMSCapabilities();

    $.ajax({
        url: "http://localhost:8080/geoserver/ows?service=wms&version=1.1.0&request=GetCapabilities", 
        success: function(response) {
            result = parser.read(response);
            var layersArray = result.Capability.Layer.Layer;
            layersNameArray = [];

            for(i=0;i<layersArray.length;i++){
                layersNameArray.push(layersArray[i].Name)
            }
        }
    }); 

    return layersNameArray
}

How can I fix that so that I get the layersNameArray variable when I call the function ?

Community
  • 1
  • 1
kaycee
  • 901
  • 1
  • 9
  • 35
  • I don't see how you're going to get any better answer than the one you linked. If you tried the solutions and they didn't work, then post your actual attempts in a question. –  Feb 19 '16 at 18:00
  • This is the real message "Embrace the asynchronous nature of JavaScript!". Javascript is full of code that takes time to execute. So instead of wanting your function to return a value, you send a request, and you define a function that handles what ever you wanted to do with that value. That function will be called when ever the service is ready (for example when Ajax returns a value). – Emmanuel Delay Feb 19 '16 at 18:07

0 Answers0