Django 'http://localhost:8000' 已被 CORS 策略阻止

Django 'http://localhost:8000' has been blocked by CORS policy

我在服务器上有一个错误(本地主机没问题)

from origin 'http://localhost:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我尝试了以下方法(关注此页面

pip install django-cors-headers

# Added at the top of MIDDLEWARE
MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',

# Added after allowed hosts
CORS_ORIGIN_ALLOW_ALL=True
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True

经过以下更改后我有

You can see the error in browser developer's mode VM87:5553 crbug/1173575, non-JS module files deprecated.

如何修复错误?

我的解决方案:

sudo vim /etc/nginx/sites-available/default
sudo service nginx restart

已更改文件

server{
    server_name www.api.site.ru api.site.ru;

    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since,X-token' always;
    if ($request_method = 'OPTIONS') {
        return 204;
    }

    location / {
         proxy_pass http://127.0.0.1:8000;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-Proto $scheme;
         proxy_set_header X-Forwarded-Proto https;
         proxy_set_header X-Forwarded-Protocol  $scheme;

         proxy_headers_hash_max_size 512;
         proxy_headers_hash_bucket_size 128;
    }

    access_log /var/log/nginx/ar_access.log;
    error_log /var/log/nginx/ar_error.log;

    listen 80; # managed by Certbot

}