Jump to content

variables in a url


elcaiaimar

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...