-1

When I am building an Angular project with MySQL and PHP, my browser console log shows CORS header ‘access-control-allow-origin’ missing.

After I have installed the CORS extension to my browser and it works fine. But I want to control and give access in my PHP code. How can I write my code to solve this problem?

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Sachin Muthumala
  • 775
  • 1
  • 9
  • 17

2 Answers2

1

You can do as @Jithin said adding this to top of you php files .

header('Access-Control-Allow-Origin: *');

But please remember unless you specify specific origin by putting * disable CORS protection and let all origins to access you file.

So its better to specify the orgin as below

header('Access-Control-Allow-Origin: https://www.test.com')

Nipun Tharuksha
  • 2,496
  • 4
  • 17
  • 40
1

We had the same problem in our project, where we ended giving up on using the CORS extension for the browser, that wasnt safe and didnt always work.

Instead we used angular CLI's built-in proxy config to reroute all calls to a remote location.

We did this by

A) creating a proxy-config.json file (see https://stackoverflow.com/a/39715785 )

B) and then run project with angular CLI using the following command: ng serve --proxy-config proxy.conf.json

This way you trick you browser into "thinking" that you are accessing data from the same location, even though you aren't. And as a bonus you avoid using browser extensions that could be malicious.