Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The "No 'Access-Control-Allow-Origin' header is present on the requested resource" error occurs when an Ajax request is made to a server that doesn't have the appropriate CORS headers enabled. This error can be resolved by adding the following PHP code to the top of the PHP file that is being requested:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

This code enables cross-origin resource sharing (CORS) on the server and allows the Ajax request to be processed without error. It should be noted, however, that enabling CORS on a server can pose security risks if not done correctly.