gpt4 book ai didi

reactjs - 仅在 chrome 上使用 corsheaders 缺少补丁方法

转载 作者:行者123 更新时间:2023-12-05 06:50:46 26 4
gpt4 key购买 nike

我有一个使用 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 请求时,我得到了这个:

Google Chrome PATCH request

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

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

Firefox PATCH request

最佳答案

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

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',
]

阅读更多:

关于reactjs - 仅在 chrome 上使用 corsheaders 缺少补丁方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66341353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com