Skip to content
Advertisement

how to solve CORS preflight error in rest api

We have two sites on one server. We created a rest api and the api backend codes are located in Site1.com. On the second site, there is a frontend that sends requests to the APIs of the first site But we encounter error 405 and this error : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://site1.com/t.php. (Reason: CORS preflight response did not succeed)

.htaccess on Site1.com :

JavaScript

API Code Site1.com/t.php :

JavaScript

Console Browser :

JavaScript

Advertisement

Answer

There is 2 sides to this question:

1- front-end : In your front-end js request, you should make sure to send data with form data.

2- back-end : In your back-end code you should directly get the request with $_POST global and not with json_decode(file_get_contents(“php://input”), true).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement