A
A
Alex2021-04-18 14:28:46
open server
Alex, 2021-04-18 14:28:46

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

1 answer(s)
V
Vladimir, 2021-04-18
@Casufi

It all depends on how you will deploy your API to production
. If in life they will be on the same domain, then it is not necessary to play with cors. You can simply configure the proxy API on webpack
https://webpack.js.org/configuration/dev-server/#d...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question