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)
-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.