-4

I tried everything, adding all the headers and changing the options too but it seems that whatever I do, the cors error still haunts me.

I get his error: CORS Missing Allow Origin

The frontend of my website is hosted on netlify and the backend on heroku. I am using react, axios, mysql, node and express. I also haven't fully set up the domain name but the website works fine except for the cors error that happens only on certain post requests. Suprisingly I think I got the preflight request working but it is also uncertain at times.

here is my express node setup:

var cors = require('cors');
app.use(cors({ origin: true, credentials: true }));
app.options('*', cors())

app.use(express.static("./public"));
app.use(express.json());
app.use(express.urlencoded({
  extended: true
}));

Here is how my axios requests look like:

Axios.post("https://backendurl" , {
                       headers : {
                            "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
                            'Content-Type': "application/json; charset=utf-8",
                            'Access-Control-Allow-Origin': '*',
                            'Access-Control-Allow-Credentials': 'true',
                            'Access-Control-Allow-Methods': "GET, POST, PUT, DELETE OPTIONS",
                            'Accept': '*',
                            // Accept: '*',
                        }, 
                        params: {
                            name: name, 
                        }
                        }).then((response) => {
            console.log(response.data);

            
        });

I also tried setting up a proxy through the allow all cors request server but it didn't work either.

If anyone has any leads that would be of interest to solve this cors issue that would be great. Especially concerning the main post request. Thanks a lot.

T P
  • 21
  • 3
  • when you've set `credentials: true`, you need to pass `withCredentials` in your axios request https://stackoverflow.com/a/43178070/10796932 – boxdox Aug 15 '21 at 07:50
  • Thanks for the tip but unfortunately even after adding this to the request, it still doesn't work and I have no idea why. – T P Aug 15 '21 at 16:35

1 Answers1

-1

What happens when you use the ip address instead? So instead of https://backendurl, use https://192.x.x.x:port/

Daedalus
  • 255
  • 1
  • 2
  • 9
  • I don't think it changed anything. – T P Aug 19 '21 at 02:01
  • Ah, that is too bad. It definitely worked for me. I have front end (FE) and backend (BE) on a box that serves locally. Some reason I was getting the same error, even tho I had cors enabled on the BE express side. I ended up just using ip addresses for ALL calls from the FE to the BE, and now it works fine from other devices on my network. – Daedalus Aug 21 '21 at 13:17