被 CORS 策略与 nginx docker 阻止



您好,我正在尝试从 API "restNations" 获取数据,但它被 CORS 策略阻止了。我不明白为什么,我试图使用 header() 函数在 index.php 文件中设置标头,但

失败了。我正在使用docker-compose文件"php,php-fpm,mysql,nginx">

我的要求 :

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(); 

}

我的 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;
}

API https://restcountries.com/v3.1/all 不允许执行 POST 方法。 将方法从POST更新为GET

最新更新