【问题标题】:ReactJS with graphql CORS Issue with django backendReactJS with graphql CORS Django后端问题
【发布时间】:2022-02-17 21:19:29
【问题描述】:

我在http://127.0.0.1:8000 上运行django 后端,我想通过reactjs 前端请求它。

如下所示,我可以在Insomnia 应用中看到成功响应。

但是,当我想要一个带有 reactjs 前端的请求时,我得到一个 CORS ERROR。

我在检查网络中检查了请求,我看到了一个类似的失眠请求负载。

【问题讨论】:

  • 试试django-cors-headers

标签: reactjs django graphql apollo-client


【解决方案1】:
  1. 安装django-cors-headers:
    pip install django-cors-headers
  2. settings.py 中添加corsheadersINSTALLED_APPS
INSTALLED_APPS = [
    ...,
    "corsheaders",
    ...,
]
  1. CorsMiddleware 添加到settings.py 中的中间件:
MIDDLEWARE = [
    ...,
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    ...,
]
  1. 现在您可以像这样在settings.py 中设置允许的来源:
CORS_ALLOWED_ORIGINS = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000",
]

更多信息请参阅link

【讨论】:

    猜你喜欢
    • 2020-03-01
    • 2021-11-14
    • 1970-01-01
    • 2021-10-24
    • 2020-11-07
    • 2020-09-15
    • 1970-01-01
    • 2021-11-25
    • 2018-06-12
    相关资源
    最近更新 更多