【问题标题】:'Access-Control-Allow-Origin' header contains multiple values '*, *', while using django-cors-headers'Access-Control-Allow-Origin' 标头包含多个值 '*, *',同时使用 django-cors-headers
【发布时间】:2016-08-15 23:09:07
【问题描述】:

我正在使用 django-cors-headers 来克服 python django 中的 cors 问题。但我得到了。

'Access-Control-Allow-Origin' 标头包含多个值 '*, *',但只允许一个值。在尝试使用来自http://localhost:8000的angularjs访问时@

这是我正在使用的 CORS 设置。

INSTALLED_APPS = INSTALLED_APPS + ['corsheaders']

MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + ['corsheaders.middleware.CorsMiddleware', 'corsheaders.middleware.CorsPostCsrfMiddleware']

CORS_ORIGIN_ALLOW_ALL = True

CORS_REPLACE_HTTPS_REFERER = True


CORS_ALLOW_HEADERS = (
        'x-requested-with',
        'content-type',
        'accept',
        'origin',
        'authorization',
        'x-csrftoken',
        'accept-encoding'
    )

CORS_ALLOW_METHODS = (
        'GET',
        'POST',
        'PUT',
        'PATCH',
        'DELETE',
        'OPTIONS'
    )

如果有人解决了这个问题,请告诉我。

【问题讨论】:

标签: python angularjs django django-rest-framework django-cors-headers


【解决方案1】:

你需要做的

MIDDLEWARE_CLASSES = (
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
)
CORS_ORIGIN_ALLOW_ALL = True #for testing.

CorsMiddlewareCommonMiddleware之上。

希望这会有所帮助。

【讨论】:

  • 我已经这样做了,但这并没有解决我的问题,我仍然收到上述错误'Access-Control-Allow-Origin' header contains multiple values '*, *',
【解决方案2】:
CORS_ORIGIN_ALLOW_ALL = False

将允许全部更改为 false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-09
    • 2016-10-02
    • 2020-02-04
    • 2019-01-08
    • 2019-06-19
    • 2016-09-30
    • 2023-03-24
    相关资源
    最近更新 更多