How to Fix "CORS Policy Blocked Origin"?

Hello, I’m building an API with PHP and I’m having trouble with CORS policy errors. When I try to access my API from a different domain, I get this error message:

Origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Can someone guide me on how to fix this CORS error? Thank you!

Hi! you can use your .htaccess file, to fix the problem. simply put the following code inside your .htaccess file. If you don’t have one then create one.

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

If you want to handle it with php just add the following line in your php code.

<?php
// Enable CORS for all domains
header("Access-Control-Allow-Origin: *");