Overcoming the “Cross-Origin Request Blocked” error in PHP

I was making an AJAX call and was getting a “Cross-Origin Request Blocked” error. It wasn’t clear where this error was originating, so I did a little research and here’s what I came up with.

When the server determines that the script making the AJAX request is on a different domain, depending on the server settings it might block the request and throw a “Cross-Origin Request Blocked” error. Nothing was wrong with my AJAX call, other than that it was originating from a different domain. In the PHP file that is responding to the AJAX call, adding the following line before any of the file output solved the issue:

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

I found the solution here: https://enable-cors.org/server_php.html

More information here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

Posted in ,

Leave a Comment

Your email address will not be published. Required fields are marked *