[GIS] Using WFS protocol in OpenLayers

geoserverjavascriptopenlayers-2wfs

Here is my code for getting a WMS layer in OpenLayers from Geoserver.

var wfs2 = new OpenLayers.Layer.Vector("WFS", {
                    strategies: [new OpenLayers.Strategy.Fixed()],
                    protocol: new OpenLayers.Protocol.WFS({          
                    version: "1.1.0",
                    url: "http://localhost:8080/geoserver/wfs",            
                    featurePrefix: "topp",
                    featureType: "states",
                    featureNS: "www.openplans.org/topp",
                    srsName: "EPSG:4326",
                    geometryName: "position"
                  })          
         });

So i get this problem in developer tools for chrome :

XMLHttpRequest cannot load http://localhost:8080/geoserver/wfs. Origin null is not allowed by Access-Control-Allow-Origin. 

in developer tools for chrome

I saw that people recommend to set up a proxy, but my .html is in the geoserver/data_dir/www folder, and don't call external urls.

I however tried it, so I created the folder cgi-bin in /usr/local/geoserver/cgi-bin/, where i put the proxy code I found here :http://trac.osgeo.org/openlayers/wiki/FrequentlyAskedQuestions#ProxyHost

And then I added in the cript of my .html this : OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";

But I then get this error in developer tools :

Failed to load resource file://localhost/cgi-bin/proxy.cgi?url=http%3A%2F%2Flocalhost%3A8080%2Fgeoserver%2Fwfs 

Probably the proxy is not in the right place, but I don't think the first problem (without problem) comes from here.

Best Answer

You should access it through http protocol. file protocol is not allowed with a WFS server. see this StackOverflow question.

To test geoserver WFS try GetCapabilities request.

For your case try this:

http://localhost:8080/geoserver/wfs?service=wfs&version=1.1.0&request=GetCapabilities.

More info at geoserver manual