我有Django应用(XXX.XXX.XXX.XXX:55002
)和React.js前端(XXX.XXX.XXX.XXX:55001
)。该应用程序可在本地完美运行,但服务器中存在CORS问题。
我可以在服务器中正确打开前端,但是当它通过HTTP将请求发送到Django应用时,则会引发错误:
无效的HTTP_HOST标头:“ XXX.XXX.XXX.XXX:55002”。您可能需要添加 “ XXX.XXX.XXX.XXX”到ALLOWED_HOSTS。
选项 http://XXX.XXX.XXX.XXX:55002/predict?&arrivalDelay=5&schedTurnd=45 400(错误请求)fetchData @ Test.js:89值@ Test.js:40 Ia @ react-dom.production.min.js:5351 Ra @ react-dom.production.min.js:5099 Aa @ react-dom.production.min.js:5066 za @ react-dom.production.min.js:5481 En @ react-dom.production.min.js:1731(index):1访问权限为 'http://XXX.XXX.XXX.XXX:55002/predict?&arrivalDelay=5&schedTurnd=45' 来自来源“ http://XXX.XXX.XXX.XXX:55001”的信息已被CORS阻止 策略:对预检请求的响应未通过访问控制 检查:它没有HTTP正常状态。
这是settings.py
的代码。如您所见,我在XXX.XXX.XXX.XXX
上添加了ALLOWED_HOSTS
:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'hfkahsf'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['XXX.XXX.XXX.XXX', 'localhost', '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',
]
MIDDLEWARE = [
'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',
]
ROOT_URLCONF = 'crida_backend.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'crida_backend.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.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/2.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/2.1/howto/static-files/
STATIC_URL = '/static/'
这是fetch
的{{1}}部分:
Test.js
答案 0 :(得分:1)
请按照以下步骤使用django-cors-headers:
pip install django-cors-headers
然后将其添加到settings.py
中的已安装应用中:
INSTALLED_APPS = (
...
'corsheaders',
...
)
添加CORS_ORIGIN_WHITELIST
到您的settings.py
CORS_ORIGIN_WHITELIST = (
'10.10.30.37:3000' # <-- update
)
或
添加CORS_ORIGIN_ALLOW_ALL
并将其设置为True
,以便它将接受来自任何域的请求。
CORS_ORIGIN_ALLOW_ALL = True
在settings.py
中添加中间件:
MIDDLEWARE = [ # Or MIDDLEWARE_CLASSES on Django < 1.10
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]