Javascript 将应用程序部署到Heroku后无法获取API数据(在本地完全工作)

Javascript 将应用程序部署到Heroku后无法获取API数据(在本地完全工作),javascript,reactjs,django,heroku,django-rest-framework,Javascript,Reactjs,Django,Heroku,Django Rest Framework,部署到Heroku后,我无法公开从Django Rest API获取数据。它在本地非常有效。当我点击heroku网页->检查->控制台时,显示“未能加载资源:net::ERR_CONNECTION_densed”&“Uncaught(in promise)TypeError:Failed to fetch” 该API公开位于heroku上,它正确地包含了我的所有数据() 我使用的是Django后端和ReactJS前端 设置.py import os from pathlib import Pat

部署到Heroku后,我无法公开从Django Rest API获取数据。它在本地非常有效。当我点击heroku网页->检查->控制台时,显示“未能加载资源:net::ERR_CONNECTION_densed”&“Uncaught(in promise)TypeError:Failed to fetch”

该API公开位于heroku上,它正确地包含了我的所有数据()

我使用的是Django后端和ReactJS前端

设置.py

import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['xxxxxxxxxxxxx.herokuapp.com', '127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    'rest_framework',

    'backend',

    'corsheaders',
]


REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny',
    ]
}

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'xxxxxxxxxxxxx.urls'


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'reactapp/build'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'xxxxxxxxxxxxx.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'reactapp/build/static'),
]   

CORS_ORIGIN_WHITELIST = [
    "http://localhost:3000", "http://127.0.0.1:8000", "https://xxxxxxxxxxxxx.herokuapp.com",
]
App.js

  fetchTasks(){
    fetch('https://xxxxxxxxxx.herokuapp.com/api')
    .then(response => response.json())
    .then(data =>
      this.setState({
        todoList:data
      })
      )
  }

解决了!我犯了愚蠢的错误。我的数据是从本地API而不是公共API获取的。我更改了代码以公开获取,但忘了在终端中运行“npm run build”。

听起来问题在于CORS(跨源资源共享)?使用CURL或POSTMAN是否有效?