在Django中启用CORS(跨源请求) [英] Enabling CORS (Cross Origin Request) in Django

查看:169
本文介绍了在Django中启用CORS(跨源请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用立交桥API http://wiki.openstreetmap.org/wiki/Overpass_API 在Django上运行的项目中使用JavaScript XMLHttpRequest,但我不断得到

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

 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). 

错误.无论我使用的是GET还是POST,以及从其他任何主机(不仅是立交桥API),都会收到此错误.

error. I get this error whether I'm using GET or POST, and from any other host, not just the overpass API.

我已经安装了django-cors-headers https://github.com/ottoyiu/django -cors-headers 并按照那里的说明进行操作,将"corsheaders"放入INSTALLED_APPS中,将"corsheaders.middleware.CorsMiddleware","django.middleware.common.CommonMiddleware"放入MIDDLEWARE_APPS中,并且我已设置

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

CORS_ORIGIN_ALLOW_ALL = true 

中的

,但似乎没有任何作用.我使用

in settings.py but nothing seems to work. I'm running it locally with

python manage.py runserver

但是我也在openshift上托管它.这些工作都没有,它们都给出了上面的错误.

but I'm also hosting it on openshift. Neither on of these work, they both give the error above.

如果我在这里遗漏任何东西,请告诉我.

Please let me know if I am missing anything here.

推荐答案

我在尝试访问托管在Django Rest FrameworkAPI时遇到了相同的问题rel ="noreferrer"> Heroku 来自我的笔记本电脑(localhost).我正在使用Django 1.10.2DRF 3.4.7python v3.4.

I was having the same problem while trying to access my Django Rest Framework API hosted at Heroku from my laptop (localhost). I am using Django 1.10.2, DRF 3.4.7 and python v3.4.

我做了pip install django-cors-headers(版本1.2.2)并将其配置为文档再说一次,同样的错误:(

I did pip install django-cors-headers (version 1.2.2) and configured it as the docs say and then, the same error again :(

继续搜索几个小时,然后打了我!

Keep searching for hours and then it hit me!

我做了pip install django-cors-middleware(版本1.3.1)而没有卸载 django-cors-headers软件包.另外,我没有触碰settings.py文件中的任何内容(它被配置为django-cors-headers设置,尽管这两个软件包没有太多差异-后者是第一个的分支).

I did pip install django-cors-middleware (version 1.3.1) without uninstalling the django-cors-headers package. 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).

(从本地主机)刷新并一切正常!

Hit refresh (from localhost) and everything worked brilliantly!

我现在能够通过 jQuery的ajax myapp.herokuapp.com中获取数据. >方法.

I was now able to fetch data from myapp.herokuapp.com via jQuery's ajax method.

这篇关于在Django中启用CORS(跨源请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆