0

I have a page that when user clicks a button the app will listen and creates a fetch call to my local server which will do a process and then redirect to an external url say google.com (I placed actually the url of my homepage just to see if it would work but nothing I get the same error (server and react app are on different ports))how do I avoid the current error that I'm facing "Access to fetch at 'http://localhost:3001/' (redirected from 'http://localhost:3000/servreq') from origin 'null' 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."

Client:----------------------------------------------

//.....
testitem(){
fetch('http://localhost:3000/payreq', {
            method: 'post',
            headers: { 'Content-Type': 'application/json'},
            body: JSON.stringify({
            })
        })
}
//......
<button className="btn btn-primary" onClick={this.testitem}>Redirect</button>
//......

Server:-----------------------------------------------

const express= require('express');
const bodyParser= require('body-parser');
const cors=require('cors');
const app=express();
app.use(bodyParser.json());
app.use(cors({origin: 'http://localhost:3001'}));
//im requesting from say localhost:3001/test/test 

app.post('/',(req,res)=>{
    res.redirect('http://localhost:3001/');
})

I have tried to do the following with no result: (I understand that the origen is null but I dont know how to fix)

-Add Headers

-concatenation method

-Wildcard method '*' (i dont think this is the appropriate plus does not work)

One attempt did work but, its just not in my opinion what should be done.

I simply generated a res.json(website to go to) in server side and on the client side just read that and use window.location=response to redirect but again I don't think it the way top go about it.If someone out there could help me I would trully appreciate it.

VericalId
  • 105
  • 12

1 Answers1

0

If you are request a page from different app server you have to tell your hosting app server accept requests from different hosting servers by adding the following lines to hosting app-server's web.xml and adding necessary jars to classpath

<filter>
         <filter-name>CORS</filter-name>
         <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>CORS</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

Necessary jars: cors-filter.jar , java-property-utils.jar Note: I excluded all link from cors filter by adding /*, you can set necessary paths. I hope that solves your problem.

mstfyldz
  • 482
  • 1
  • 6
  • 12
  • I'm using JavaScript as my coding language, does this solution still applies?. I don't know where to get access to the xml files. Sorry I'm new to web developing – VericalId Dec 31 '18 at 17:26
  • Add these to your web.xml and add jar files to Web-Inf/lib directory – mstfyldz Dec 31 '18 at 17:30