elcaiaimar 0 Posted March 16, 2015 Report Share Posted March 16, 2015 Hello! I'm working in a viewer with openlayers and with geoserver and I want to filter layers by a variable. I'm not going to ask anything about geoserver nor openlayers. My problem is about put variables in a url. To obtain geoserver layers from openlayers you have to call them by a url and to filter them you have to use CQL_FILTER. (Filter to shows only what the user wants). My viewer works fine if I put a simple value to filter: (gid_cuenca_id is a db column) var imbornalesSource = new ol.source.ServerVector({ format: new ol.format.GeoJSON(), loader: function(extent, resolution, projection) { var url = 'http://localhost:8080/geoserver/cia/wfs?service=WFS' + '&version=1.1.0&request=GetFeature&typename=cia:imbornales&PROPERTYNAME=gid_cuenca_id&CQL_FILTER=gid_cuenca_id=1&' + 'outputFormat=text/javascript&format_options=callback:imbornalesloadFeatures' + '&srsname=EPSG:3857'; $.ajax({ url: url, dataType: 'jsonp' }); }, projection: 'EPSG:3857' }); However, if I try to put a variable in the url, I receive an error: Uncaught SyntaxError: Unexpected token <I wanted to know if there is any way to put the variable's value as if it was a simple value. var myVar = 1;var tramosSource = new ol.source.ServerVector({ format: new ol.format.GeoJSON(), loader: function(extent, resolution, projection) { var url = 'http://localhost:8080/geoserver/cia/wfs?service=WFS' + '&version=1.1.0&request=GetFeature&typename=cia:tramos&PROPERTYNAME=gid_cuenca_id&CQL_FILTER=gid_cuenca_id=myVar&' + 'outputFormat=text/javascript&format_options=callback:tramosloadFeatures' + '&srsname=EPSG:3857'; $.ajax({ url: url, dataType: 'jsonp' }); }, projection: 'EPSG:3857' }); Thank you very much!! Quote Link to post Share on other sites
justsomeguy 1,135 Posted March 16, 2015 Report Share Posted March 16, 2015 Just add the value of the variable to the string. You're just building a string there, so your second example adds the text "myVar". You can just concatenate the value of the variable into the string instead. Quote Link to post Share on other sites
elcaiaimar 0 Posted March 16, 2015 Author Report Share Posted March 16, 2015 I've tried to put that: var myVar = 1;var tramosSource = new ol.source.ServerVector({ format: new ol.format.GeoJSON(), loader: function(extent, resolution, projection) { var url = 'http://localhost:808...wfs?service=WFS' + '&version=1.1.0&request=GetFeature&typename=cia:tramos&PROPERTYNAME=gid_cuenca_id&CQL_FILTER=gid_cuenca_id="myVar"&' + 'outputFormat=text/javascript&format_options=callback:tramosloadFeatures' + '&srsname=EPSG:3857'; $.ajax({ url: url, dataType: 'jsonp' }); }, projection: 'EPSG:3857' }); But I have the same problem: Uncaught SyntaxError: Unexpected token < <ows:ExceptionReport xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/ows http://localhost:8080/geoserver/schemas/ows/1.0.0/owsExceptionReport.xsd"> <ows:Exception exceptionCode="InvalidParameterValue"> <ows:ExceptionText>Illegal property name: myVar</ows:ExceptionText> </ows:Exception></ows:ExceptionReport> Quote Link to post Share on other sites
justsomeguy 1,135 Posted March 16, 2015 Report Share Posted March 16, 2015 That's not the correct way to concatenate the variable, look at the the rest of the URL that you're concatenating. The error message isn't related to that though, the error message might be because you're telling it to expect JSON and it's returning XML instead. Quote Link to post Share on other sites
elcaiaimar 0 Posted March 16, 2015 Author Report Share Posted March 16, 2015 Sorry, but I'm trying everything and don't get anything. Can you tell me how you would do it with my code? I'm still a newbie in JS Thank you for your fast answers! Quote Link to post Share on other sites
justsomeguy 1,135 Posted March 16, 2015 Report Share Posted March 16, 2015 You could look up Javascript variable concatenation without needing to wait for me, but you end the string and use + to concatenate another value. var url = 'http://localhost:808...wfs?service=WFS' + '&version=1.1.0&request=GetFeature&typename=cia:tramos&PROPERTYNAME=gid_cuenca_id&CQL_FILTER=gid_cuenca_id=' + myVar + '&' +... Quote Link to post Share on other sites
elcaiaimar 0 Posted March 17, 2015 Author Report Share Posted March 17, 2015 That is! It works! Sorry but I didn't find the solution. Thank you very much! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.