Cross Origin or CORS Issue

We often get the CORS error when we try to run some code from localhost or XAMPP. The following steps will help to solve it. Please note you will need access to server side code or server configurations. 

  1. Set Access-Control-Allow-Origin to “*” in the code
  2. Set Header set Access-Control-Allow-Headers to “*” in the code 
  3. If the above didn’t work then add the same to .htaccess
  4. If still not solved then set the same in Apache / HTTPD configuration

In my case setting it in the code didn’t work. So it set it in the .htaccess but it also didn’t work. I was getting the error “Reason: header ‘access-control-allow-headers’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response“. The point to note is CORS Preflight. Which indicated that the error happened before the request reached the code. So I thought to set it in Apache config and it worked.

In my case the server had ISPConfig, so I set it through ISPConfig instead of changing the actual Apache configuration file.
The configuration needs to be put under “Sites” -> “<Site Name>” ->  “Options” -> “Apache Directives”.
The exact lines to be put
Header set Access-Control-Allow-Origin “*”
Header set Access-Control-Allow-Headers “*”

 

Leave a Reply