Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Resolve Reason: CORS header ‘Access-Control-Allow-Origin’ missing with PHP.

Tiempo de lectura: < 1 minuto

Reading time: < 1 minute

If executing a call to our REST API returns an error message “Cross-origin request blocked: The same origin policy does not allow reading of remote resources” or “Reason: CORS header ‘Access-Control-Allow-Origin’ missing” or “(Reason: CORS request failed)”.

Most likely, our server-side REST calls do not have the necessary headers to allow them to be used by different origins. If our server is PHP, we need to add the following header in our call (file or function) depending on the framework we use.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Origin: *');
 header('Access-Control-Allow-Origin: *');

With this header, we allow access to our API from any origin (publicly).

Other useful headers for PHP are:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
header('Content-type: application/json');
header('Content-type: application/json');
 header('Content-type: application/json');

Indicates that the response is in JSON format.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
header('Access-Control-Allow-Headers: Content-Type, X-AUTH');
header('Access-Control-Allow-Headers: Content-Type, X-AUTH');
 header('Access-Control-Allow-Headers: Content-Type, X-AUTH');

Indicates that you allow custom headers.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
 header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');

Indicates the type of requests that can be made to this endpoint (you can choose one or several).

0

Leave a Comment