How to enable CORS with PHP

Issue

I have an angular 4 app talking to a php script that resides on WAMP (www folder) they are both on my local machine. I get this error logged in the browser when i hit the script.

No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:4200' is therefore not allowed access. 
The response had HTTP status code 404.

I know its because i am trying to access from localhost to localhost that this problem occurs.

I did a bit of reading and it appears i need to fix it from wamp server. Not sure how to do that on wamp.

Solution

As commented, the problem is that you need to allow all origins from the script.

//at the very begining of your php script.
header('Access-Control-Allow-Origin: *'); 

Answered By – yBrodsky

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published