Angular and XAMPP CORS Policy

Viewed 1643

I am trying to send some requests from my angular app to my local xampp server, but the requests are blocked due to CORS Policy.

I have already added following line to the httpd.conf file:

Header set Access-Control-Allow-Origin *

I have also added an .htaccess file to my htdocs folder with following content:

Header set Access-Control-Allow-Origin *

I have also added this to my php file:header("Access-Control-Allow-Origin: *"); but this had no impact as well.

My angular app runs on localhost:4200 and the xampp runs on localhost:80

I know allowing every origin is a great security issue, but it is only for testing purpose.

2 Answers

I think you need a few more headers from your server side, namely Access-Control-Allow-Methods and Access-Control-Allow-Headers.

header("Access-Control-Allow-Origin: http://localhost:4200");
header("Access-Control-Allow-Methods: GET,POST,PUT,OPTIONS");
header("Access-Control-Allow-Headers:*");

Try modifying your http.conf like this

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"

RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
Related