<legend id='822RZ'><style id='822RZ'><dir id='822RZ'><q id='822RZ'></q></dir></style></legend>

<tfoot id='822RZ'></tfoot>

    1. <small id='822RZ'></small><noframes id='822RZ'>

        <i id='822RZ'><tr id='822RZ'><dt id='822RZ'><q id='822RZ'><span id='822RZ'><b id='822RZ'><form id='822RZ'><ins id='822RZ'></ins><ul id='822RZ'></ul><sub id='822RZ'></sub></form><legend id='822RZ'></legend><bdo id='822RZ'><pre id='822RZ'><center id='822RZ'></center></pre></bdo></b><th id='822RZ'></th></span></q></dt></tr></i><div id='822RZ'><tfoot id='822RZ'></tfoot><dl id='822RZ'><fieldset id='822RZ'></fieldset></dl></div>
          <bdo id='822RZ'></bdo><ul id='822RZ'></ul>

        如何解决我的 Django API 的 CORS 问题?

        时间:2023-07-06

      1. <tfoot id='0Ndoc'></tfoot>

              • <bdo id='0Ndoc'></bdo><ul id='0Ndoc'></ul>
              • <legend id='0Ndoc'><style id='0Ndoc'><dir id='0Ndoc'><q id='0Ndoc'></q></dir></style></legend>

                <small id='0Ndoc'></small><noframes id='0Ndoc'>

                <i id='0Ndoc'><tr id='0Ndoc'><dt id='0Ndoc'><q id='0Ndoc'><span id='0Ndoc'><b id='0Ndoc'><form id='0Ndoc'><ins id='0Ndoc'></ins><ul id='0Ndoc'></ul><sub id='0Ndoc'></sub></form><legend id='0Ndoc'></legend><bdo id='0Ndoc'><pre id='0Ndoc'><center id='0Ndoc'></center></pre></bdo></b><th id='0Ndoc'></th></span></q></dt></tr></i><div id='0Ndoc'><tfoot id='0Ndoc'></tfoot><dl id='0Ndoc'><fieldset id='0Ndoc'></fieldset></dl></div>
                  <tbody id='0Ndoc'></tbody>
                  本文介绍了如何解决我的 Django API 的 CORS 问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法在我的 Django API 中解决 CORS 问题.当我调用此 API 时,我收到错误:

                  I cannot solve CORS problem in my Django API. When I make a call to this API, I get error:

                  从源头访问 'http://localhost:8000/''http://localhost' 已被 CORS 策略阻止:响应预检请求未通过访问控制检查:否请求中存在Access-Control-Allow-Origin"标头资源.如果不透明的响应满足您的需求,请设置请求的模式为no-cors"以获取禁用 CORS 的资源.

                  Access to fetch at 'http://localhost:8000/' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

                  为了启用 CORS,我做了 pip install django-cors-headers 并将以下代码添加到 settings.py:

                  To enable CORS, I did pip install django-cors-headers and added the following code to settings.py:

                  INSTALLED_APPS = [
                      'django.contrib.admin',
                      'django.contrib.auth',
                      'django.contrib.contenttypes',
                      'django.contrib.sessions',
                      'django.contrib.messages',
                      'django.contrib.staticfiles',
                      'corsheaders',
                  ]
                  
                  MIDDLEWARE_CLASSES = [
                      'corsheaders.middleware.CorsMiddleware',
                      'django.middleware.common.CommonMiddleware',
                      'django.middleware.security.SecurityMiddleware',
                      'django.contrib.sessions.middleware.SessionMiddleware',
                      'django.middleware.common.CommonMiddleware',
                      'django.middleware.csrf.CsrfViewMiddleware',
                      'django.contrib.auth.middleware.AuthenticationMiddleware',
                      'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
                      'django.contrib.messages.middleware.MessageMiddleware',
                      'django.middleware.clickjacking.XFrameOptionsMiddleware',
                  ]
                  
                  CORS_ORIGIN_WHITELIST = [
                      'localhost:80',
                      'localhost:8000',
                      '127.0.0.1:8000'
                  ]
                  

                  我应该说我在 Docker 上运行我的项目.这是 docker-compose.yml:

                  I should say that I run my project on Docker. This is docker-compose.yml:

                  version: '2'
                  
                  services:
                    django-docker:
                      build:
                        context: .
                        dockerfile: Dockerfile.django
                      container_name: my.django
                      image: my-django
                      ports:
                        - 8000:8000
                  
                    webapp-docker:
                      build:
                        context: .
                        dockerfile: Dockerfile.webapp
                      container_name: my.webapp
                      image: my-web
                      ports:
                        - 80:80
                  

                  推荐答案

                  需要在 settings.py 的中间件类中添加 corsheaders.middleware.CorsMiddleware 中间件:

                  You need to add corsheaders.middleware.CorsMiddleware middleware to the middleware classes in settings.py :

                  MIDDLEWARE_CLASSES = (
                      'corsheaders.middleware.CorsMiddleware',
                      'django.middleware.common.BrokenLinkEmailsMiddleware',
                      'django.middleware.common.CommonMiddleware',
                      #...
                  )
                  

                  您的中间件类中有重复的 django.middleware.common.CommonMiddleware.

                  You have duplicate django.middleware.common.CommonMiddleware in your middleware classes.

                  然后,您可以通过添加以下设置为所有域启用 CORS:

                  You can then, either enable CORS for all domains by adding the following setting:

                  CORS_ORIGIN_ALLOW_ALL = True
                  

                  或仅对指定域启用 CORS:

                  Or Only enable CORS for specified domains:

                  CORS_ORIGIN_ALLOW_ALL = False
                  
                  CORS_ORIGIN_WHITELIST = (
                      'http://localhost:8000',
                  )
                  

                  这篇关于如何解决我的 Django API 的 CORS 问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有 AWS cognito 的无服务器框架生成 CORS 错误 下一篇:django.db.utils.OperationalError: (1071, '指定的密钥太长;最大密钥长度

                  相关文章

                  <i id='Jwa0h'><tr id='Jwa0h'><dt id='Jwa0h'><q id='Jwa0h'><span id='Jwa0h'><b id='Jwa0h'><form id='Jwa0h'><ins id='Jwa0h'></ins><ul id='Jwa0h'></ul><sub id='Jwa0h'></sub></form><legend id='Jwa0h'></legend><bdo id='Jwa0h'><pre id='Jwa0h'><center id='Jwa0h'></center></pre></bdo></b><th id='Jwa0h'></th></span></q></dt></tr></i><div id='Jwa0h'><tfoot id='Jwa0h'></tfoot><dl id='Jwa0h'><fieldset id='Jwa0h'></fieldset></dl></div>
                1. <tfoot id='Jwa0h'></tfoot>

                  1. <small id='Jwa0h'></small><noframes id='Jwa0h'>

                      <bdo id='Jwa0h'></bdo><ul id='Jwa0h'></ul>
                    <legend id='Jwa0h'><style id='Jwa0h'><dir id='Jwa0h'><q id='Jwa0h'></q></dir></style></legend>