简体   繁体   English

在 Django 中启用 CORS(跨源请求)

[英]Enabling CORS (Cross Origin Request) in Django

I'm trying to make use of the overpass API http://wiki.openstreetmap.org/wiki/Overpass_API with a JavaScript XMLHttpRequest in a project running on Django but I keep getting the我正在尝试在 Django 上运行的项目中使用带有 JavaScript XMLHttpRequest 的立交桥 API http://wiki.openstreetmap.org/wiki/Overpass_API ,但我一直得到

 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.google.com/accounts/ClientLogin. (Reason: CORS header 'Access-Control-Allow-Origin' missing). 

error.错误。 I get this error whether I'm using GET or POST, and from any other host, not just the overpass API.无论我是使用 GET 还是 POST,以及来自任何其他主机,而不仅仅是立交桥 API,我都会收到此错误。

I've installed django-cors-headers https://github.com/ottoyiu/django-cors-headers and followed the instructions there, putting 'corsheaders' into INSTALLED_APPS, and 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', into MIDDLEWARE_APPS and I've set我已经安装了 django-cors-headers https://github.com/ottoyiu/django-cors-headers并按照那里的说明进行操作,将“corsheaders”放入 INSTALLED_APPS 和“corsheaders.middleware.CorsMiddleware”、“django.middleware” .common.CommonMiddleware',进入 MIDDLEWARE_APPS 并且我已经设置

CORS_ORIGIN_ALLOW_ALL = true 

in settings.py but nothing seems to work.在 settings.py 中,但似乎没有任何效果。 I'm running it locally with我在本地运行它

python manage.py runserver

but I'm also hosting it on openshift.但我也在 openshift 上托管它。 Neither on of these work, they both give the error above.这些工作都没有,它们都给出了上面的错误。

Please let me know if I am missing anything here.如果我在这里遗漏了什么,请告诉我。

I was having the same problem while trying to access my Django Rest Framework API hosted at Heroku from my laptop ( localhost ).我在尝试从我的笔记本电脑( localhost )访问Heroku托管的Django Rest Framework API 时遇到了同样的问题。 I am using Django 1.10.2 , DRF 3.4.7 and python v3.4 .我正在使用Django 1.10.2DRF 3.4.7python v3.4

I did pip install django-cors-headers (version 1.2.2) and configured it as the docs say and then, the same error again :(我做了pip install django-cors-headers (version 1.2.2) 并按照文档的说明配置了它,然后又出现了同样的错误:(

Keep searching for hours and then it hit me!继续搜索数小时,然后它击中了我!

I did pip install django-cors-middleware (version 1.3.1) without uninstalling the django-cors-headers package.pip install django-cors-middleware (version 1.3.1)没有卸载django-cors-headers包。 Also I didn't touch a thing in my settings.py file (it was configured as the django-cors-headers settings, although these two packages do not have many differences - the latter is a fork of the first).此外,我没有触及我的settings.py文件中的任何内容(它被配置为django-cors-headers设置,尽管这两个包没有太多区别 - 后者是第一个的分支)。

Hit refresh (from localhost) and everything worked brilliantly!点击刷新(来自本地主机),一切都运行良好!

I was now able to fetch data from myapp.herokuapp.com via jQuery 's ajax method.我现在可以通过jQuery的 ajax方法从myapp.herokuapp.com获取数据。

Remember to put the 'corsheaders.middleware.CorsMiddleware' in the top of your list, and also the 'django.middleware.common.CommonMiddleware' is already a standard middleware记得把 'corsheaders.middleware.CorsMiddleware' 放在列表的顶部,而且 'django.middleware.common.CommonMiddleware' 已经是一个标准的中间件

MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
 ]

CORS_ORIGIN_ALLOW_ALL = true CORS_ORIGIN_ALLOW_ALL = 真

should be:应该是:

CORS_ORIGIN_ALLOW_ALL = True CORS_ORIGIN_ALLOW_ALL = 真

T capital letter for True. T大写字母表示 True。 Add additional required middleware添加额外的必需中间件

MIDDLEWARE = ['corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware', ]

and register 'corsheaders', to INSTALLED_APPS.并将“corsheaders”注册到 INSTALLED_APPS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM