仅在 chrome 上缺少带有 corsheaders 的补丁方法

Missing Patch method with corsheaders only on chrome

我有一个使用 corsheaders 包的 Django 应用程序,它在 settings.py 中,如下所示:

INSTALLED_APPS = [ ..., corsheaders, ...]
...
MIDDLEWARE = [
    # on top
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    ...
]
...
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True

当我尝试在 OPTIONS 方法之后对 Google Chrome 执行 PATCH 请求时,我得到了这个:

缺少 Access-Control-Allow-Methods PATCH并且下一个请求因 CORS 方法错误而失败。

但我在 Firefox 上尝试了相同的方法,它按预期工作。

您似乎需要明确设置允许的来源,而不是使用通配符,即*:

CORS_ALLOWED_ORIGINS = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000"
]

此外,设置所有 HTTP 动词:

CORS_ALLOW_METHODS = [
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
]

阅读更多: