Answer the question
In order to leave comments, you need to log in
How to solve CORS issue when requesting axios to OpenServer?
Hello.
I want to make a request from a react application to openServer, and I ran into a CORS problem:
This is how everything works
, here is my request:
axios.post(" auth/test.php ", {"name": "Alex"});
here is the php file I am accessing:
<?php
echo 'Hello';
but if I start accessing variables from $_POST - I get a CORS error
<?php
$name = $_POST['name'];
echo $name;
Found a solution on the net. Headers need to be added to php file:
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Credentials: true');
or so
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Credentials: true');
header('Content-type: json/application');
or so
header('Access-Control-Allow-Origin': '*');
header('Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE');
header('Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept');
Nothing changes.
How can this be fixed?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question