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',前面。
参考: