I'm currently using Angular4 with django-nonrel as the backend. Got the CORS error at the beginning and installed django-cors-headers to give django support for CORS. I had to install version 1.1.0 because django-nonrel supports up to django 1.6.11. Everything was working perfectly so far for several weeks, but after I turned off my laptop today I started getting the CORS error again.
我目前正在使用Angular4和django-nonrel作为后端。在开始时遇到CORS错误并安装了django-cors-headers以给予对CORS的django支持。我不得不安装1.1.0版,因为django-nonrel支持django 1.6.11。到目前为止,一切都运行良好,持续数周,但在我今天关闭笔记本电脑后,我又开始收到CORS错误了。
Django
ConnectionFailure: [Errno 10061] No connection could be made because the target machine actively refused it
ConnectionFailure:[Errno 10061]无法建立连接,因为目标计算机主动拒绝它
Angular
XMLHttpRequest cannot load http://127.0.0.1:8000/pgla/project_list. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:4200' is therefore not allowed access. The response had HTTP status code 500
XMLHttpRequest无法加载http://127.0.0.1:8000/pgla/project_list。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许来源“http://127.0.0.1:4200”。响应的HTTP状态代码为500
I verify the installing instructions for the django-cors-headers module and everything is correct. Here are my settings:
我验证了django-cors-headers模块的安装说明,一切正确。这是我的设置:
MIDDLEWARE_CLASSES = (
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
)
CORS_ORIGIN_ALLOW_ALL = True
And here is the hearder paremeter for Angular:
这是Angular的听众参数:
new Headers({'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'}),
Any help will be much appreciated.
任何帮助都感激不尽。
Thanks in advanced.
先谢谢了。
1 个解决方案
#1
0
Django corsheaders works with django 1.8 and above. Please upgrade your django version and then try again.
Django corsheaders适用于django 1.8及以上版本。请升级您的django版本,然后重试。
Requirements Tested with all combinations of:
要求测试所有组合:
Python: 2.7, 3.6 Django: 1.8, 1.9, 1.10, 1.11, 2.0
Python:2.7,3.6 Django:1.8,1.9,1.10,1.11,2.0
#1
0
Django corsheaders works with django 1.8 and above. Please upgrade your django version and then try again.
Django corsheaders适用于django 1.8及以上版本。请升级您的django版本,然后重试。
Requirements Tested with all combinations of:
要求测试所有组合:
Python: 2.7, 3.6 Django: 1.8, 1.9, 1.10, 1.11, 2.0
Python:2.7,3.6 Django:1.8,1.9,1.10,1.11,2.0