Solution for cors Issue

Avatar
  • updated
  • Answered

Hi, I'm trying to solve cors issue by using htaccess file.

The goal is to enable access to the server from local development environment.

I tried the code below but not working. Any advice would be appreciated. 

Header add Access-Control-Allow-Origin [redacted]

Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"

Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

Avatar
JT S.

A system administrator I asked about this stated this CORS on Apache article may prove helpful as well.

Avatar
alan1ststraw
Quote from Yano

Hi alan1ststraw,

It's the server side issue not frontend. 

You have to write some code for fixing cors issue in your backend file(php or node or python).

If your backend is express app, it would be like this:

app.use(function(req, res, next) { 

res.header("Access-Control-Allow-Origin", '*'); 

res.header("Access-Control-Allow-Credentials", true); 

res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); 

res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json'); 

next();});

Node app is supported by Heroku, aws, and other servers.

Thanks Yano. I knew all the front end hacks would not be a good long term solution, especially since I want to attempt to profit from my application (as simple as it is). I was contemplating php while taking a shower today (of all places. where I do my best thinking). You just made it that much more easier for me.  Now I can stop banging my head against the wall trying to come up with a JS solution.  It will have to be either a PHP or Python solution because my hosting plan does not allow node access. Thanks for the rapid response. All is not lost :)

Avatar
Yano
Quote from alan1ststraw

What did you do to fix it? (if you don't mind me asking) I have been experiencing the same problem and have tried all of the client side fixes but none seem to work on a consistent basis. Firefox is the most Forgiving along with Edge but Chrome, Safari and Opera are very unforgiving.  Forget about trying to use any ES6 JavaScript on Safari (unless I am missing something). Any advice will be appreciated.  Thanks in advance.

Hi alan1ststraw,

It's the server side issue not frontend. 

You have to write some code for fixing cors issue in your backend file(php or node or python).

If your backend is express app, it would be like this:

app.use(function(req, res, next) { 

res.header("Access-Control-Allow-Origin", '*'); 

res.header("Access-Control-Allow-Credentials", true); 

res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); 

res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json'); 

next();});

Node app is supported by Heroku, aws, and other servers.

Avatar
alan1ststraw
Quote from Yano

Hi, It's fixed now. Thank you.

What did you do to fix it? (if you don't mind me asking) I have been experiencing the same problem and have tried all of the client side fixes but none seem to work on a consistent basis. Firefox is the most Forgiving along with Edge but Chrome, Safari and Opera are very unforgiving.  Forget about trying to use any ES6 JavaScript on Safari (unless I am missing something). Any advice will be appreciated.  Thanks in advance.

Avatar
Yano

Hi, It's fixed now. Thank you.

Avatar
John-Paul B
  • Answered

Hello Yano,

Thank you for contacting us about solving a cors issues using .htaccess. I recommend using a tool such as RegExr to build and test your syntax. If your problems persist contact Live Support so they can look at your specific account and troubleshoot further.

Thank you

John-Paul