Hola, estoy tratando de obtener datos de una API "restCountries" y está bloqueado por la política CORS. No entiendo por qué, he intentado establecer encabezados en el archivo index.php con la función header(), pero ha fallado.
Estoy trabajando con un archivo docker-compose "php, php-fpm, mysql, nginx"
Mi petición :
const getlistOfAllCountries = async () => { // const myHeaders = new Headers() const responseFromWorldApi = await fetch("https://restcountries.com/v3.1/all", { method: 'POST', headers : { 'Access-Control-Allow-Origin' : '*', "Content-Type": "application/json" } }); const listOfAllCountries = await responseFromWorldApi.json(); }mi archivo default.conf
server { server_name ~.*; location / { root /usr/src/app; try_files $uri /index.php$is_args$args; } location ~ ^/.+\.php(/|$) { client_max_body_size 50m; fastcgi_pass php:9000; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /usr/src/app/public$fastcgi_script_name; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; # # Om nom nom cookies # add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; # # Custom headers and headers various browsers *should* be OK with but aren't # add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; # # Tell client that this pre-flight info is valid for 20 days # add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; } } error_log /dev/stderr debug; access_log /dev/stdout; }La API https://restcountries.com/v3.1/all no permite realizar el método POST. Actualice su método de POST a GET