Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Possible Duplicate:
Setup own Openlayers Proxyhost
Error WFS Request, PROXY problems

I have this error:

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

in my google chrome console when i make a WFS request. I use apache tomcat 6 and geoserver which run on 8081 and 8080 port. I read that this error is related to proxy.

How must to resolve my problem? I run my application on Windows 7 machine.

share|improve this question

marked as duplicate by Devdatta Tengshe, iant, whuber Nov 4 '12 at 20:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

1 Answer 1

Due to Javascript security rules, you are not allowed to make an XMLHttpRequest to another server. Even two servers running on the same machine, but different ports have this limitation.

What you need to do, is to have a cgi proxy on your tomcat webserver, and use the OpenLayers.ProxyHost to point to it.

The OpenLayers site has a FAQ Answer for this issue :

Why do I need a ProxyHost?

Due to security restrictions in Javascript, it is not possible to retrieve information from remote domains via an XMLHttpRequest.

Classes like WFS and GeoRSS use XMLHTTPRequest to get their data. If they are querying a remote server (anything other than the machine hosting your page), you must install a proxy script somewhere web accessible on that machine. See below for how to set up your own ProxyHost.

If the OpenLayers.ProxyHost variable is not set to a valid proxy host, requests are sent directly to the remote servers. In most cases, the result will be a security exception, although this exception often occurs silently. How do I set up a ProxyHost?

An example proxy host script is available here: trunk/openlayers/examples/proxy.cgi

For the standard Apache configuration, you would place proxy.cgi into your /usr/lib/cgi-bin/ directory.

Once a proxy host script has been installed, you must then edit the OpenLayers.ProxyHost variable to match that URL.

Given the above standard Apache configuration:

OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";

If you have done something like this, you should be able to visit:

 http://YourDomain.example.com/cgi-bin/proxy.cgi

The resulting content at that page should be the openlayers.org website.

If you get a 404 error instead, either the proxy script is not in the right location, or your webserver is not configured correctly.

After you have copied over the proxy file to your server, don't forget to edit the allowedHosts array.

share|improve this answer
    
Hi devdatta. I did all step, i added folder cgi-bin in apache-tomcat and save in it proxy.cgi (path: ...../apache-tomcat-6.0.33/bin/cgi-bin/proxy.cgi). In proxy.cgi in allowedHosts variable i added 'localhost','localhost:8080','localhost:8081'. In OpenLayer.js i modified OpenLayer.ProxyHost from "" to"/cgi-bin/proxy.cgi?url=". Now, if i go on localhost:8081/cgi-bin/proxy.cgi i have the 404 error. Why this? Where i mistake? –  Riccardo Oct 30 '12 at 8:19
    
In the tomcat documentation, it is mentioned, that the CGI is diabled by default. Look at point 8 in this article, on how to enable it: oreilly.com/pub/a/java/archive/tomcat-tips.html?page=2 –  Devdatta Tengshe Oct 30 '12 at 8:46
    
Hi Dev!Thanks for your reply. Still problem. I have found this guide: tomcat.apache.org/tomcat-6.0-doc/cgi-howto.html for tomcat 6. In short, in file C:\Program Files\apache-tomcat-6.0.33\conf\web.xml have uncomment <servlet-name>cgi.. and <servlet-mapping> ... When i restart tomcat in eclipse console have the exception follow: InvocationTargetException ...Caused by: java.lang.SecurityException: Servlet of class org.apache.catalina.servlets.CGIServlet is privileged and cannot be loaded by this web application –  Riccardo Oct 30 '12 at 20:34
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.