I’ve been struggling with this error after migrating a site from Google Cloud in which I was using Nginx and now I changed the hosting to an Apache server.
The website is in 2 sections front-end (hosted on Vercel NextJS) and the backend which is a WordPress site used as an API with GraphQL (this is the one I migrated) I have used the GraphQL plugin and now the request from the front-end is getting this error:
Access to XMLHttpRequest at [backend domain] from origin [front end domain] has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
I have added this to the .htaccess file:
Header add Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET,PUT,POST,DELETE"
Header set Access-Control-Allow-Headers "Content-Type"
I’m using Cloudflare in both the backend and the frontend.
Don’t know what else to do. I have tried to go back to the previous hosting and it works perfectly so I don’t know what’s the problem! Please help! Thank you! 🙂
3
Answers
It's fixed! Apparently, I had to flush the permalinks by just saving the permalinks settings: Settings > Permalinks
I found the solution here: https://github.com/wp-graphql/wp-graphql/issues/213
Add this filter in functions.php file in theme and make sure SSL enabled
This is not really safety but it works for me
I’ve put this to .htaccess file