Geoserver SLD – How to Apply an SLD Saved on Geoserver to OpenLayers 3

geoserveropenlayerssld

I am a complete beginner in JavaScript, XML and coding in general (so if there's anyway for me make my question more clear please tell me).

I have loaded a WMS onto my OpenLayers 3 map. The relevant code looks like this

var vectorLayer3 = new ol.layer.Tile({
    source: new ol.source.TileWMS({
        preload: Infinity,
        url: 'http://localhost:8080/geoserver/wms',
        serverType:'geoserver',
        params:{
            'LAYERS':"jazz", 'TILED':false, 'STYLES':'mostbasic'
        }
    })

}); 
    map.addLayer(vectorLayer3);

The map loads, but I get the generic style instead the 'most basic' style. I can get the style to show properly using the preview on WMS, so I don't think the problem is with the SLD. Looking at the documentation for ol.source.TileWMS it looks like there should be someway to get the style like I tried, but I am having trouble seeing it.

If I am completely off base, would following this tutorial be the best way to go, should I use a vector, WFS or is there a better strategy?

Best Answer

The answer to my question is actually quite simple.

var vectorLayer3 = new ol.layer.Tile({
    source: new ol.source.TileWMS({
        preload: Infinity,
        url: 'http://localhost:8080/geoserver/wms',
        serverType:'geoserver',
        params:{
            LAYERS:'jazz', TILED:false, STYLES:'mostbasic'
        }
    })

}); 
    map.addLayer(vectorLayer3);

As you can see, I needed to get rid of the single quotes around the params, and add quotes around the names of the paramaters I was using.