0

A friend created a Rest API in Genexus, which was written to Java. We are using Gnok to serve your files from the local machine without the need to pay for a private VPS.

However, when I try to consume their endpoints from my web application made in React, I get the following error:

Access to fetch at 'https://9f64-2800-a4-2933-4100-60e8-3344-52b6-563d.sa.ngrok.io/nortscapeJavaEnvironment/rest/wsGetEmpresas' from origin 'http://localhost: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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I think it is something that should be solved from the server to be able to support different request origins, but how can we do it in GeneXus/Java?

  • You dont have to do anything in GeneXus. You are missing the configuration for Cross Origin on the application server (apache/tomcat...). – Chon Aug 31 '22 at 17:25

2 Answers2

0

First of all you have to configure your Tomcat to add 'Access-Control-Allow-Origin' header. This post in SO describes how to accomplish this. Second, you have to add the "--host-header=rewrite" option to ngrok, something like this:

C:\> ngrok http --host-header=rewrite 8080

8080 is the port where tomcat is listening.

And that's all.

-1

You have not allowed Cross site access in you htaccess file you can allow access by placing the following code in your htaccess file

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

More information on this question can be found here:

htaccess Access-Control-Allow-Origin

HelloWorld
  • 133
  • 1
  • 15