方式一:

在中间件中

def process_response(self, request, response):
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Headers'] = "content-type"
    response['Access-Control-Allow-Methods'] = "DELETE,PUT"
    return response

方式二:

使用 django-cors-headers 组件

pip install django-cors-headers

settings.py

INSTALLED_APPS = [
    ...
    'corsheaders',
    ...
 ]
 MIDDLEWARE_CLASSES = (
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
)

#跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ()
 
CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)
 
CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
)

相关文章:

  • 2022-02-07
  • 2022-02-01
  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
猜你喜欢
  • 2022-03-08
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
  • 2021-11-21
  • 2021-06-09
  • 2022-12-23
相关资源
相似解决方案