环境安装
pip install django-cors-headers
settings.py 修改配置
INSTALLED_APPS = [
"···",
"corsheaders", # 加上这个
"···",
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware", # 放到第一行
'···',
]
# CORS Config
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_HEADERS = ('*')
from django.views.decorators.csrf import csrf_exempt
在视图函数上添加 @csrf_exempt
@csrf_exempt
def index(request):
return "Hello World!"