No 'Access-Control-Allow-Origin' Django

Access to XMLHttpRequest at 'http://127.0.0.1:8000/add' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Django框架Restangular发送API请求,跨源资源共享的问题。
解决方法:
 
pip install django-cors-headers
INSTALLED_APPS = (
...
'corsheaders',   
...
)
MIDDLEWARE_CLASSES
= (
...   
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
)
 
CORS_ORIGIN_ALLOW_ALL = True  
 
注意 
'corsheaders.middleware.CorsMiddleware',  需要在'django.middleware.common.CommonMiddleware',前面。
 
参考:
posted @ 2020-02-28 18:20  船长博客  阅读(604)  评论(0编辑  收藏  举报
永远相信美好的事情即将发生!