Enable CORS with wamp on windows 8

2019-01-18 14:14发布

问题:

I have a cross domain request problem with an application I'm doing. I really spent hours looking for a solution on how to enable CORS with wamp (localhost) but nothing worked for me.

I have Apache 2.4.9 on Windows 8.1. I have enable the headers, I tried to put

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

in a .htaccess and in the http.conf as well as countless other variations.

If anyone had a solution that'd be awesome!

回答1:

You must also activate the Apache Headers module.

Using the wampmanager menus do this :-

wampmanager -> Apache -> Apache modules -> headers_module

Make sure this is ticked, if its not, click that menu item and wait a few seconds while WampServer restarts Apache.



回答2:

I had the same problem and i solved it with these 3 steps:

1) in Apache config file (for me the path was C:\wamp\bin\apache\apache2.4.18\conf\httpd.conf) add the line: Header set Access-Control-Allow-Origin "*" in the content of the <Directory> tag:

DocumentRoot "c:/wamp/www"
<Directory "c:/wamp/www/">
    Options +Indexes +FollowSymLinks
    Header set Access-Control-Allow-Origin "*"
    AllowOverride all
    Require local
</Directory>

2) activate the "headers_module" in apache's modules (it will also restart your apache server, effectively applying the change made in step 1)

3) clear your browser cache (I am using chrome and i was told the best way to "hard clear" the cache was to go in the developper tools -> Networks tab -> right click -> clear browser cache) (by the way, clearing the browser cache is often useful when debugging in chrome)

Now it should work. Good luck !