3

I am using a GeoServer docker image from here and running a docker container on CentOS Linux. GeoServer Docker

I have mounted Geoserver data directory as external and able to successfully run the GeoServer

I am able to get WFS request without any issue. However when I do Post on WFS. I am getting a CORS error. I am running my web server and docker on same server so IP is same but GeoServer runs 8080 and my NodeJS React app runs on 3000.

Access to XMLHttpRequest at 'http://XXX:8080/geoserver/VPS/ows' from origin 'http://XXX:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Tomcat in the docker container is CORS enabled by default. I tried to enable the CORS in GeoServer webapp folder also.

Still I am getting the same error.

What am I missing here to solve this CORS issue

And my Post call looks like below from React Axios

axios({
  method: 'post',
  headers: { "Access-Control-Allow-Origin": "*", "X-Frame-Options": "SAMEORIGIN", "Content-Type": 'text/xml' },
  url:configData.gs_transactUrl,
  crossdomain: true,
  service: 'WFS',
  version: '1.0.0',
  dataType: 'xml',
  processData: false,
  RequestMethod: "POST",
  contentType: 'text/xml',
  typeName:configData.gs_typeName,
  data: myXMLdata
}).then(function (response) {
//do something here
}
Vince
  • 20,017
  • 15
  • 45
  • 64
Senthil S
  • 31
  • 3
  • 1
    As the message says, you probably need to set the Access-Control-Allow-Origin header. More on this can be found here: https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Access-Control-Allow-Origin – swiss_knight Jul 14 '21 at 12:45
  • @swiss_knight I tried to add CORS header on my ExpressJS so for request it passes the headers. But Still its getting same error. Access-Control-Allow-Origin I set to * and also IP but no success

    const cors=require('cors');

    const corsOptions={ origin: '*', optionsSuccessStatus: 200, credentials:true, preflightContinue:true, };

    – Senthil S Jul 14 '21 at 12:56
  • are you sure tomcat has CORS on by default? I don't think it does. see https://gis.stackexchange.com/questions/370605/allow-cors-on-geoserver-with-tomcat-and-nginx – Ian Turton Jul 14 '21 at 13:07
  • @IanTurton, In the docker image (mentioned above in link) which I am using, it is enabled by default. Also I have verified that it has CORS filters in place – Senthil S Jul 14 '21 at 13:14
  • Then as @swiss_knight says you aren't correctly setting the Access-control-allow-origin header – Ian Turton Jul 14 '21 at 13:34
  • @IanTurton and swiss_knight, I have edited my question to include the Post request i am using in app. I tried to use * and also the server IP in header but nothing was successful – Senthil S Jul 14 '21 at 13:45

1 Answers1

1

in order to enable the CORS on GeoServer you will need to tweak this option on the GEOSERVER_JAVA_OPTS variable of the .env file:

-DGEOSERVER_CSRF_DISABLED=true

Thomaz
  • 11
  • 1