Solucionar problema PHP: CORS
Budget: $10 – $20 USD
Hola.
Cuento con un script de php, alojado en apache con cPnginx instalado:
<?php
//El script tiene configurados los siguientes encabezados.
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('content-type: application/json; charset=utf-8');
echo "Test...";
//Para fines de resolver el problema, el resto del script en no es necesario
?>
El script está alojado en el dominio: https://curl.tecno-group.mx
Teniendo abierto el dominio https://www.akky.mx en una pestaña de Chrome, desde la consola de Chrome ejecuto estos comandos:
console.clear();
const urlAPI = "https://curl.tecno-group.mx";
var Data = {
"data01": "dato01",
"data02": "dato02",
"data03": "dato03",
"data04": "dato04",
"data05": "dato05",
"data06": "dato06"
};
let fetchData = {
method: 'POST',
body: JSON.stringify(Data),
headers: {"Content-Type": "application/json"}
};
fetch(urlAPI, fetchData)
.then(resp => resp.json())
.then(data => {
console.log(data)})
.catch(err => {
console.error(err);
});
Y la respuesta que obtengo es esta:
Access to fetch at 'https://curl.tecno-group.mx/' from origin 'https://www.akky.mx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Lo que necesito es ayuda para configurar correctamente Access-Control-Allow-Origin y permitir el acceso desde el dominio que yo decida, como por ejemplo: https://www.akky.mx
Cuento con un script de php, alojado en apache con cPnginx instalado:
<?php
//El script tiene configurados los siguientes encabezados.
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('content-type: application/json; charset=utf-8');
echo "Test...";
//Para fines de resolver el problema, el resto del script en no es necesario
?>
El script está alojado en el dominio: https://curl.tecno-group.mx
Teniendo abierto el dominio https://www.akky.mx en una pestaña de Chrome, desde la consola de Chrome ejecuto estos comandos:
console.clear();
const urlAPI = "https://curl.tecno-group.mx";
var Data = {
"data01": "dato01",
"data02": "dato02",
"data03": "dato03",
"data04": "dato04",
"data05": "dato05",
"data06": "dato06"
};
let fetchData = {
method: 'POST',
body: JSON.stringify(Data),
headers: {"Content-Type": "application/json"}
};
fetch(urlAPI, fetchData)
.then(resp => resp.json())
.then(data => {
console.log(data)})
.catch(err => {
console.error(err);
});
Y la respuesta que obtengo es esta:
Access to fetch at 'https://curl.tecno-group.mx/' from origin 'https://www.akky.mx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Lo que necesito es ayuda para configurar correctamente Access-Control-Allow-Origin y permitir el acceso desde el dominio que yo decida, como por ejemplo: https://www.akky.mx