elcaiaimar Posted March 16, 2015 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!! Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 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. Link to comment Share on other sites More sharing options...
elcaiaimar Posted March 16, 2015 Author 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> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 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. Link to comment Share on other sites More sharing options...
elcaiaimar Posted March 16, 2015 Author 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! Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 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 + '&' +... Link to comment Share on other sites More sharing options...
elcaiaimar Posted March 17, 2015 Author Share Posted March 17, 2015 That is! It works! Sorry but I didn't find the solution. Thank you very much! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now